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:
Launch this app and open MainActivity.
Launch ActivityA from MainActivity.
Return to the Home screen while ActivityA is in the foreground.
Launch an unrelated app.
Observe that ActivityA.onDestroy() is called.
I would like to understand the following:
What mechanism in Android's task management or Activity lifecycle causes ActivityA to be destroyed?
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: