1

In my app, I navigate from MainActivity to Activity B. I then press the HOME button and then click on the app icon again to maximize it.

Instead of onResuming my Activity B, it goes back to MainActivity.

This makes sense because in my AndroidManifest.xml, I'm using the normal intent-filters to launch the MainActivity when you click on its icon:

    <activity
        android:name=".MainActivity"
        android:launchMode="singleTask"
        android:label="MyApp"
        android:windowSoftInputMode="adjustPan">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

I was checking popular apps like facebook and twitter, none of them seem to experience this problem - when you navigate from your facebook feed to a friends page and then press the HOME button. Then you click on the app icon again and it is still on your friend's page.

This only seems to affect the app when I click on the app icon. If for example go from MainActivity to Activity B, press HOME button and then press the SQUARE button to show all my apps running in the background, select my app that is in the background to maximize it again, the app will return back correctly to Activity B.

How do I make sure that when you click on the app icon, it onResumes Activity B instead?

Simon
  • 19,658
  • 27
  • 149
  • 217
  • 4
    Remove android:launchMode="singleTask" from your Manifest. Description here http://developer.android.com/guide/topics/manifest/activity-element.html#lmode – Febi M Felix Jan 01 '16 at 10:21
  • Add if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) == Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) { finish(); } in your MainActivity's onCreate(); – tiny sunlight Jan 01 '16 at 10:21
  • The launchtask was set like that because I'm integrating deeplinks which will not work if I remove it. Nonetheless I will still try it out later and get back to u. I will also try out ur suggestion tiny sunlight. – Simon Jan 01 '16 at 10:24
  • Febi M Felix will you please convert your comment into an answer. Your suggestion is correct and my app now works as intended. – Simon Jan 01 '16 at 10:31
  • Does this also happen if you force stop the app and then start it again by clicking on the app icon on the home screen? If not, You are probably seeing this nasty Android bug: http://stackoverflow.com/a/16447508/769265 – David Wasser Dec 28 '16 at 15:22
  • @Simon How did you handle deeplinks if you removed singletask, its necessary for deeplinks – abhishek maharajpet Jul 11 '22 at 10:11

1 Answers1

0

Remove android:launchMode="singleTask" from manifest

From Android launchMode doc,

"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

From Android back stack singleTask doc,

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.

Hope this will help you.

Community
  • 1
  • 1
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256