SingleTask activity is created multiple times within the same process

2019-10-15 android android-activity android-tv launchmode android-activitymanager

I have a launcher app which has a singleTask activity as the main entry point. When the user navigates away to another activity or to a 3rd party app and then hits the home button, then this activity should be brought to the front. However what I experience is that for the first home button press only, another instance is created instead (a new task is created, onCreate() is called). In the meantime the old task is still alive, containing the original instance of this activity, but it is impossible to navigate back to that task/activity or to bring it to the foreground.

After the first home button press, the next home button press brings the 2nd instance of this activity to the foreground. Not sure why not the very first instance's onNewIntent() method is called for the first time... So this only happens once, after that always the 2nd instance's onNewIntent() method is called. This means that the original activity will be not accessible..

I tried to bring the task to the foreground, nothing was happening... Like if it never existed (but the task is there with the activity, it is not killed at any point). I can find the task from code and also using a shell script. It contains the original activity

This is happening on Android TV (Os: Pie). Any idea what can be the reason for this? I do not really understand how this is happening... BTW the result is the same if I set the activity to singleInstance.

The activity looks like this:

<activity
            android:name=".activities.MainActivity"
            android:excludeFromRecents="true"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme">
            <intent-filter android:priority="2">
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.HOME"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.ALL_APPS"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

I tried alternating the above attributes (priority, excludeFromRecents, taskAffinity), also removed them completely, they had no effect...

The home button press sends the following intent:

  • action: "android.intent.action.MAIN"
  • category: "android.intent.category.HOME"
  • component: the above activity

it has also some extras but I do not think it is relevant

Answers

You are experiencing this nasty long-standing Android bug:

Re-launch of Activity on Home button, but...only the first time

You say in a comment that it doesn't happen with real users. That is not actually true. If a real user would install your app from the Play store and then launch it immediately (click the OPEN APP button after installation) then the problem would be reproduced exactly as you describe it.

Related