Activity with excludeFromRecents="true", launchMode="singleTask", and a custom taskAffinity is destroyed when another app is launched


After displaying ActivityA, which is declared with the following attributes in AndroidManifest.xml, launching an unrelated app causes ActivityA.onDestroy() to be called.

<activity
android:name=".ActivityA"
android:excludeFromRecents="true"
android:taskAffinity="${applicationId}.acta"
android:launchMode="singleTask" />

Steps to reproduce:

  1. Launch this app and open MainActivity.

  2. Launch ActivityA from MainActivity.

  3. Return to the Home screen while ActivityA is in the foreground.

  4. Launch an unrelated app.

  5. Observe that ActivityA.onDestroy() is called.

I would like to understand the following:

  1. What mechanism in Android's task management or Activity lifecycle causes ActivityA to be destroyed?

  2. Is there a way to prevent ActivityA from being destroyed when another app is launched while keeping the Manifest attributes shown above?

A minimal reproducible example is available here. It contains only two activities, MainActivity and ActivityA:
https://github.com/Y-Joron/DestroyActivityInBg

A video demonstrating the issue is included in the GitHub repository's README.

The issue was reproduced on the following devices:

  • Pixel 8 (Android 16)
  • F-51E (Android 15)
0
Aug 12 at 10:19 AM
User Avatararikui
#java#android#kotlin

No answer found for this question yet.