Skip to content
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

Merged
merged 8 commits into from
Feb 23, 2022

Conversation

Susko3
Copy link
Member

@Susko3 Susko3 commented Feb 19, 2022

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:

  • Realme 6
  • LG G7 Fit

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 to GameHost.AllowExitingAndroid and set it to false.

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).

This changes the default behaviour to exit/minimize the app when pressing back.
/// 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));
Copy link
Member

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.

Copy link
Member Author

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?

@pull-request-size pull-request-size bot added size/M and removed size/L labels Feb 21, 2022
Comment on lines 611 to 612
if (!CanSuspendToBackground)
throw new InvalidOperationException($"Tried to call {nameof(SuspendToBackground)}() on a host that doesn't support it (see {nameof(CanSuspendToBackground)}).");
Copy link
Collaborator

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.

Copy link
Member

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;
Copy link
Collaborator

@bdach bdach Feb 21, 2022

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

@peppy peppy left a 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.

Comment on lines 611 to 612
if (!CanSuspendToBackground)
throw new InvalidOperationException($"Tried to call {nameof(SuspendToBackground)}() on a host that doesn't support it (see {nameof(CanSuspendToBackground)}).");
Copy link
Member

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.

@peppy peppy requested a review from bdach February 22, 2022 08:39
Copy link
Collaborator

@bdach bdach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appears to work

@peppy peppy merged commit 952fd4d into ppy:master Feb 23, 2022
@Susko3 Susko3 deleted the android-back branch February 23, 2022 11:35
@Susko3 Susko3 mentioned this pull request May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants