3

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

keybee
  • 1,498
  • 20
  • 32
  • Are you using `taskAffinity`? – Dhaval Oct 15 '19 at 13:29
  • Tried both with and without it, no effect – keybee Oct 15 '19 at 13:54
  • Why did you add `android:excludeFromRecents="true"` though? That will prevent the activity from showing in the task switcher, so you cannot effectively return to the task if it's not visible. (I know you've mentioned them but still, seems strange). – Martin Marconcini Oct 16 '19 at 11:58
  • That should be not relevant as this app is the launcher on the device and users can return via the home button from anywhere. Also I tried removing that attribute, did nothing... When the device starts, the launcher starts and this activity also starts. So the app is basically always running and this activity is also always running. – keybee Oct 16 '19 at 12:01
  • 1
    How do you launch the app the very first time after installation? You are probably seeing this nasty Android bug: https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508#16447508 – David Wasser Oct 18 '19 at 11:54
  • yeah, I figured out in the meantime that probably android studio is messing with me as it was not reproducable in a live environment with real users... I did not know though that this issues is there for 10 years... that is just.. wow... I do not event find words. I will test what happens after the app updates and summarize my findings (I hope this issue only bugs development and does not occur during normal usage of an app) – keybee Oct 19 '19 at 12:34
  • 1
    BTW I wanted to reach out to you for this particular issue because I saw you are pretty good (also) at this topic, but you found the question anyways, so thx! – keybee Oct 19 '19 at 12:50

2 Answers2

2

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

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>
Rado
  • 133
  • 2
  • 7