-
Notifications
You must be signed in to change notification settings - Fork 420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a way to exit/minimize the app on Android when back is pressed #5044
Conversation
This changes the default behaviour to exit/minimize the app when pressing back.
osu.Framework/Platform/GameHost.cs
Outdated
/// If <c>true</c>, pressing back will return to the home screen (putting the app in background and suspending it). | ||
/// If <c>false</c>, the back button will be treated as <see cref="InputKey.Escape"/>. | ||
/// </remarks> | ||
public readonly AggregateBindable<bool> AllowExitingAndroid = new AggregateBindable<bool>((a, b) => a & b, new Bindable<bool>(true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to rename to AllowSuspendingAndroid
(AllowSuspendToBackground
without the android suffix maybe?) as it isn't really an exit operation. Not 100% on having android specific variables in here, but I guess it'll have to do for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checked and the Android docs seem to call it "stopping the activity" and "moving to background". So AllowSuspendToBackground
could work. Maybe there's a better name that would encompass the "when back is pressed" part of this.
How about AllowNativeBackNavigation
?
osu.Framework/Platform/GameHost.cs
Outdated
if (!CanSuspendToBackground) | ||
throw new InvalidOperationException($"Tried to call {nameof(SuspendToBackground)}() on a host that doesn't support it (see {nameof(CanSuspendToBackground)})."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This throw is a bit strange because you can bypass it entirely by overriding SuspendToBackground()
and just not calling base
. Note that Exit()
does not have the same issue because that one's not virtual.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that this shouldn't throw. If anything having a success
return value would be best.
/// <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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dunno how to feel about this - is there any chance that android could just use the exit flow rather than invent a new one? I'm not sure much value is added upon having them both upon seeing the implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the context of the osu!-side changes, having it be merged into Exit()
seems wrong, as it's not really exiting. It also has different UX implications, and you can't treat Android's "exit" as a desktop exit. Eg. osu! exits all game screens, and only then exits the host, which absolutely does not work with Android.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing to note is that we may be able to use this behaviour on desktop for the "hide to tray" functionality (aka "boss key") when we get to adding that back. Although minimise may be a closer mapping for the suspend action on desktop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems much better structurally.
osu.Framework/Platform/GameHost.cs
Outdated
if (!CanSuspendToBackground) | ||
throw new InvalidOperationException($"Tried to call {nameof(SuspendToBackground)}() on a host that doesn't support it (see {nameof(CanSuspendToBackground)})."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that this shouldn't throw. If anything having a success
return value would be best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
appears to work
Relevant: ppy/osu#16899.
This should live at framework, as it's useful for any app that wants to follow Android best practices for the back button.
MoveTaskToBack()
available since API 1. Calling this will put the activity in the background and return to the last app (eg. the home screen if the game was opened from there, or the file manager if osu! was opened from beatmap import).Tested on:
Breaking changes
Pressing the back key on Android will now exit/minimize the app and return to home screen.
To restore the old behaviour of treating the back key as
InputKey.Escape
, add your own bindable toGameHost.AllowExitingAndroid
and set it tofalse
.To be in line with mobile UX, your app should allow exiting when it's on the initial screen (eg. when backing out is no longer possible / when backing out would close the app on desktop platforms).