keybee
keybee

Reputation: 1496

SingleTask activity is created multiple times within the same process

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:

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

Upvotes: 3

Views: 1929

Answers (3)

Shaoshuai Xu Steven
Shaoshuai Xu Steven

Reputation: 1

For those who continue to encounter this issue, here's a solution based on a similar problem I faced during development.

My issue:

  1. I installed the home app I developed on an Android phone.
  2. In the existing default launcher, I clicked on the icon in the app list to start it.
  3. I set it as the default home app (I did this through Device Policy Manager, but it can also be done by requesting the user to choose).
  4. I launched any application from my own launcher and then pressed the home button.
  5. Bug appeared - a second instance of the app was created.

My solution:

private fun setAsHomeApp() {
    // The reason of the issue is a home app has to be launched correctly(by pressing Home instead of clicking app icon)
    // First set our app as the default home
    // ....
    // Then finish it since the current one is launched by the old home
    finish()
    // Lastly we do the magic by relaunching it with home intent
    val launchHomeIntent = Intent(Intent.ACTION_MAIN).apply {
        addCategory(Intent.CATEGORY_HOME)
        flags = Intent.FLAG_ACTIVITY_NEW_TASK
    }
    startActivity(launchHomeIntent)
}

Upvotes: 0

Rado
Rado

Reputation: 151

I was struggling with the exactly same issue for the last couple of hours and I've read many similar Q&A about this topic/bug on StackOverflow. No solution really worked for me until, out of mere curiosity, I did the following.

If you're building a launcher and using onNewIntent() in your MainActivity (although OP doesn't mention that he's using this method), then simply comment out this line:

super.onNewIntent(intent);

Uninstall your app and install it again.

I don't know how this works, but EVEN when you then uncomment the very same line, it still behaves in a proper manner, that is: the app has really only single instance of its main activity all the time. And how I know that? My MainActivity is doing some database operations, and previously I saw in my Logcat that these operations were done twice every time, but now they are done only once.

And BTW my MainActivity tag in AndroidManifest.xml is nothing special:

    <activity
            android:name=".activities.MainActivity"
            android:exported="true"
            android:excludeFromRecents="true"
            android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Upvotes: 0

David Wasser
David Wasser

Reputation: 95568

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.

Upvotes: 2

Related Questions