8

I have just started insuring my apps work with Android 12. What I notice is that for (I believe) all of them when the back button is pressed, then onPause() is called, but not onDestroy(). This means the app is still running.

Previously, onDestroy() was called after onPause() when the back button was pressed.

It is a definite problem for ones running a background service that has (actually, must have) a notification. Stopping the service is typically done in onDestroy() and certainly not in onPause(). Since onDestroy() is not called, the service continues running and the notification remains up. If you select the notification, it brings up the [running] app again. There is no way to stop the service and hence the notification, except by killing the process via the overview (left) button.

I also notice that if an app with a service crashes, then the notification stays up. This is surely not desired behavior.

You can, and apparently have to, kill the process with the overiew (left) soft button).

I can get around this by implementing onBackPressed:

@Override
public void onBackPressed() {
    // This seems to be necessary with Android 12
    // Otherwise onDestroy is not called
    Log.d(TAG, this.getClass().getSimpleName() + ": onBackPressed");
    finish();
    super.onBackPressed();
}

but that seems like a kludge.

For other apps maybe it doesn't matter they are still running or not. However, it is unexpected behavior.

Added later: This is apparently intentional and is a change from previous behavior. It is mentioned in this article (curtesy of @ianhanniballake):

https://developer.android.com/about/versions/12/behavior-changes-all#back-press

I have several apps, and I have verified it is happening with all of them on Android 12. It is causing problems with those that have services.

The implied question for this issue is what to do about it. I personally am implementing onBackPressed for those and leaving the others be.

Kenneth Evans
  • 2,179
  • 19
  • 26
  • 2
    Did you see the [Behavior changes for all apps in Android 12: Root activities are not finished on back press docs](https://developer.android.com/about/versions/12/behavior-changes-all#back-press)? – ianhanniballake Mar 05 '22 at 21:03
  • "Stopping the service is typically done in onDestroy() and certainly not in onPause()." -- it feels like you should be stopping the service when the activity *starts*, not when the activity *finishes*. A service is pointless when you are in the foreground. So, when the user taps the notification and brings up the UI, stop the service at that point, until such time as conditions require you to have the service running again. – CommonsWare Mar 05 '22 at 22:38
  • @ianhanniballake Thanks. No, I didn't find that in my searching. It isn't in the life cycle article yet either. It does seem to be true and causes problems in many apps as I mentioned. I am only implementing onBackPressed if there are problems, like services that should not be running if the user intended to quit. – Kenneth Evans Mar 07 '22 at 02:50

2 Answers2

0

I am not sure if this should be an answer or a comment, so, given the size of the text, I decided to go with answer.

The solution I came up with was forcing onDestroy() to be called, by finishing the activity from within onStop() in some circumstances.

In my case, I use onDestroy() to persist a few things. I know there are better ways of doing this, but that is how the code was, and this quick workaround fixed the issue, at least temporarily:

@Override
protected void onStop() {
    // Try to differentiate a home button press from the screen being turned off
    final PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
    if (powerManager != null && powerManager.isInteractive()) {
        if (webViewHost != null && !webViewHost.isPaused()) {
            // My old/existing logic (for Android < 12)
            finish();
        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
            // New workaround for Android 12 onwards
            finish();
        }
    }

    super.onStop();
}

This is the project's repo, already pointing at the line where the workaround has been created.

carlosrafaelgn
  • 831
  • 1
  • 16
  • 19
0

As per the new changes in Android 12, back button on root/home activity is now minimize the app instead of close entirely. Hence it calls only onPause() and onStop() functions not onDestroy(). So I called finish() function from inside onBackPressed() function like this.

override fun onBackPressed() {
    super.onBackPressed()
    finish()
}

This could be a simple and quick workaround to match the behaviour with below Android 12. However the good practice is to optimise the app as per the Android 12 guidelines mentioned here

Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46