Skip to content

Commit

Permalink
Merge pull request #5044 from Susko3/android-back
Browse files Browse the repository at this point in the history
Add a way to exit/minimize the app on Android when back is pressed
  • Loading branch information
peppy authored Feb 23, 2022
2 parents e72e170 + 39d4b01 commit 952fd4d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
7 changes: 7 additions & 0 deletions osu.Framework.Android/AndroidGameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ protected override void SetupConfig(IDictionary<FrameworkSetting, object> defaul

public override bool CanExit => false;

public override bool CanSuspendToBackground => true;

public override bool OnScreenKeyboardOverlapsGameWindow => true;

protected override TextInputSource CreateTextInput() => new AndroidTextInput(gameView);
Expand Down Expand Up @@ -89,5 +91,10 @@ public override IResourceStore<TextureUpload> CreateTextureLoaderStore(IResource

public override VideoDecoder CreateVideoDecoder(Stream stream)
=> new AndroidVideoDecoder(stream);

public override bool SuspendToBackground()
{
return gameView.Activity.MoveTaskToBack(true);
}
}
}
2 changes: 0 additions & 2 deletions osu.Framework.Tests.Android/TestGameActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// See the LICENCE file in the repository root for full licence text.

using Android.App;
using Android.OS;
using Android.Views;
using osu.Framework.Android;

namespace osu.Framework.Tests.Android
Expand Down
23 changes: 23 additions & 0 deletions osu.Framework/Platform/GameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,17 @@ public abstract class GameHost : IIpcHost, IDisposable
/// <summary>
/// Whether this host can exit (mobile platforms, for instance, do not support exiting the app).
/// </summary>
/// <remarks>Also see <see cref="CanSuspendToBackground"/>.</remarks>
public virtual bool CanExit => true;

/// <summary>
/// Whether this host can suspend and minimize to background.
/// </summary>
/// <remarks>
/// This and <see cref="SuspendToBackground"/> are an alternative way to exit on hosts that have <see cref="CanExit"/> <c>false</c>.
/// </remarks>
public virtual bool CanSuspendToBackground => false;

/// <summary>
/// Whether memory constraints should be considered before performance concerns.
/// </summary>
Expand Down Expand Up @@ -583,12 +592,26 @@ private set
/// <summary>
/// Schedules the game to exit in the next frame.
/// </summary>
/// <remarks>Consider using <see cref="SuspendToBackground"/> on mobile platforms that can't exit normally.</remarks>
public void Exit()
{
if (CanExit)
PerformExit(false);
}

/// <summary>
/// Suspends and minimizes the game to background.
/// </summary>
/// <remarks>
/// This is provided as an alternative to <see cref="Exit"/> on hosts that can't exit (see <see cref="CanExit"/>).
/// Should only be called if <see cref="CanSuspendToBackground"/> is <c>true</c>.
/// </remarks>
/// <returns><c>true</c> if the game was successfully suspended and minimized.</returns>
public virtual bool SuspendToBackground()
{
return false;
}

/// <summary>
/// Schedules the game to exit in the next frame (or immediately if <paramref name="immediately"/> is true).
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions osu.Framework/Testing/TestBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ private void load(Storage storage, GameHost host, AudioManager audio)
interactive = host.Window != null;
config = new TestBrowserConfig(storage);

exit = host.Exit;
if (host.CanExit)
exit = host.Exit;
else if (host.CanSuspendToBackground)
exit = () => host.SuspendToBackground();

audio.AddAdjustment(AdjustableProperty.Frequency, audioRateAdjust);

Expand Down Expand Up @@ -320,7 +323,7 @@ protected override bool OnKeyDown(KeyDownEvent e)
switch (e.Key)
{
case Key.Escape:
exit();
exit?.Invoke();
return true;
}
}
Expand Down

0 comments on commit 952fd4d

Please sign in to comment.