My launcher activity has the launchMode attribute set to singleTask due to certain requirements.

<activity
    android:name=".map.MapsActivity"
    android:launchMode="singleTask"
    android:screenOrientation="portrait"
    android:theme="@style/MapScreenTheme"
    android:windowSoftInputMode="adjustPan">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

The problem I'm facing is that if I open another activity -> press home -> Click on app icon in launcher application -> It opens the MapActivity and not the activity that was previously open.

This however does not happen if I navigate to the app via the recents menu. Then the newly opened activity stays on top.

Can someone please explain what is happening here with regards to the backstack and why is the ActivityManagerService not taking into account that the app process already exists and yet decides to start the launcher app and clear the backstack and not simply bring the app forward?

This issue can be observed in a small sample app created here - https://github.com/abhiank/SingleTaskActivity

4

There are 4 best solutions below

10
Николай Гольцев On

There is some solution for case when your main activity would be launched only by other activities within your own application:

You can remove launchMode attribute from your main activity definition in manifest. And just pass Intent.FLAG_ACTIVITY_NEW_TASK to every intent that opens your MainActivity, for example like this:

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Be aware that behaviour of Intent.FLAG_ACTIVITY_NEW_TASK or launchMode="singleTask" within your own application only works in conjunction with taskAffinity attribute on activity definition in manifest.

I hope it will help you a little.

3
akashzincle On

I think, When you click on app icon again, It opens the Launcher Activity on top of your previously opened Activity. What you can do is apply a simple check whether there are any activity in the backstack apart from this, then finish this launcher activity, It will reveal previously opened screen. Try the below link. https://stackoverflow.com/a/38450232/3497972

check these links too, they have other ways to maintain screen when launched app icon, when app is in background

How to make an android app return to the last open activity when relaunched?

Android app restarts when opened by clicking app icon

Determine when application icon is clicked to launch the app in android

What is the difference between launch app from "recent apps" and tapping app icon

3
David Wasser On

Sounds to me like you are seeing this nasty long-standing Android bug

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

To test whether that is the case, please kill your app (settings->apps->your app->force quit). Then launch your app by tapping the app icon on the HOME screen. Do whatever you need to do to launch another Activity into the task. Press HOME. Now tap the icon on the HOME screen again. This should bring your task to the foreground in the same state you left it in.

3
SpiritCrusher On

I think this is the desired behavior of SingleTask launch mode. Keeping Launcher Activity as SingleTask has its consequences.

I can not think of an exact solution for this problem but some workaround will do the Job.

Adding the link below which i have mentioned in comment to go through.

Android: bug in launchMode="singleTask"? -> activity stack not preserved