1

Android 12 introduced the behavior where the last app you had active are continuing to run when inside the recent menu. I'm wondering if there's a flag or something to put in AndroidManifest file to prevent this behavior as it's conflicting with some logics in our app (which works fine for Android < 12). I've googled but it's hard to find unless you know exactly what this "feature" is called.

Steps:

  1. Start app
  2. Open recent menu
  3. Observe that you can interact and that the app is still running as if you had it open/active

Why is this a problem? Because a user is now able to force quit the app (swiping it away) without entering the "paused" state in our game (using Unity) meaning some save logic won't run. This can be worked around in one way or another, but for now I would like to just pause the app in recent menu if possible (our app has zero reason for being active in recent menu).

EDIT: As @WyattL mentioned in his answer, android:excludeFromRecents="true" might work, but it will cause drop in playtime and retention of the game and would prefer to have a more proper solution to this "unnecessary" feature of Android 12.

Whyser
  • 2,187
  • 2
  • 20
  • 40
  • 1
    I'm not an android dev so I'm probably way off here.... but hasn't this always been possible by long pressing home then swiping to kill the application? https://stackoverflow.com/a/9226244/1092820 – Ruzihm Mar 24 '22 at 18:13
  • He’s saying that in other Android versions it would get (presumably) an OnApplicationPause() call when you long-pressed Home, and could use that to save the game state before it was swiped away. Now it’s continuing to run with no (known) indication that anything has changed, apparently. – Sven Viking Mar 25 '22 at 00:08
  • Oops, I didn’t see your link. You’re right, it does sound like this issue has always existed on certain phones, while others do trigger the OnPause event because their recent apps menu is a separate Activity. – Sven Viking Mar 25 '22 at 10:28
  • 1
    I think this in an [XY problem](https://xyproblem.info/) where the most helpful solutions will involve accepting the fact that the player may force quit the game at any moment without warning and designing the application accordingly. – Ruzihm Mar 25 '22 at 18:24
  • Seems like it. Sad if there really is often no way to exit gracefully for no good reason though (and perhaps more importantly, no way to avoid frustration for players by preventing them from dying while in the process of switching apps). You'd need to allow for things like battery disconnections either way, but that sort of rare event wouldn't warrant the frame hitches and battery drain to repeatedly save the game state defensively for example. – Sven Viking Mar 25 '22 at 19:40
  • (That's assuming from the question that a swipe-based termination provides no time to exit gracefully, which I don't know to be true first-hand. The player death situation would apply either way though.) – Sven Viking Mar 25 '22 at 19:44
  • 1
    I realize that Ruzihm (regarding the XY problem), but the game was created 5 years ago with lots of things not being perfect and intertwined. Changing the saving logic will take lots of time and testing and was/am hoping that this feature is (un)documented somewhere but that I just don't know what it's called. – Whyser Mar 25 '22 at 20:21
  • @Whyser I just checked and it doesn't seem like there's any sort of quit event on a swipe termination, but as a long shot, have you checked whether OnApplicationFocus(false) is called at any point? – Sven Viking Mar 25 '22 at 20:24
  • 1
    Funny that you should mention OnApplicationFocus(false). Just this moment I saw a thread about it being called when user touches any native UI. Gonna try on Monday, thanks for the added suggestion! (If you wanna contest for the bounty, please create an answer) – Whyser Mar 25 '22 at 20:35

2 Answers2

0

According to some brief research, did you try adding

    <activity>
      ...
      android:excludeFromRecents="true"
      android:label=""
      ...
    </activity>

to the AndroidManifest.xml?

WyattL
  • 21
  • 1
  • 1
    While I think this is a somewhat good suggestion, removing the app completely from recent menu would probably impact session/retention time of the game too much. :( – Whyser Mar 24 '22 at 08:17
0

I can't be sure without testing on every phone as it seems the issue varies by device (thanks Ruzihm), but if opening the Recent Apps screen generates an OnApplicationFocus() call, this would provide a solution.

In an active MonoBehaviour:

private void OnApplicationFocus( bool focus )
{
    if( focus ) {
        UnpauseLogic();
    }
    else {
        PauseLogic();
        SaveLogic();
    }
}

(It might also be worth trying OnApplicationQuit() in case it's called on specific devices during a swipe termination, but in my own tests it was never called.)

Sven Viking
  • 2,660
  • 21
  • 34
  • 1
    I've tested it and as hypothesized it seems to trigger OnApplicationFocus(false) when entering the the recent menu. Thanks! – Whyser Mar 28 '22 at 08:46
  • No problem -- knowing about this will likely be of use to me too, so thanks for the question! – Sven Viking Mar 28 '22 at 10:14