Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is launcher activity with launchMode="singleTask" always pushed to top of backstack even if another activity was on top? [duplicate]

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

like image 667
vepzfe Avatar asked Apr 09 '20 09:04

vepzfe


People also ask

Which launch mode to use to open an activity in a new activity stack?

Standard This is the default launch mode of activity (If not specified). It launches a new instance of an activity in the task from which it was launched.

What happens to the back stack when you switch between activities?

If the user presses or gestures Back, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.

What is the default activity launch mode *?

Standard: This is the default launch mode of activity. If you don't set any launch mode to your activity, it will use the standard mode by default. It creates a new instance of activity every time even if activity instance is already present.

How do I delete activity from stack?

The easiest way is to give the LoginActivity a “android:noHistory = true” attribute in the manifest file. That instructs Android to remove the given activity from the history stack thereby avoiding the aforementioned behavior altogether.


2 Answers

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

like image 78
ADM Avatar answered Oct 26 '22 01:10

ADM


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

like image 33
akashzincle Avatar answered Oct 26 '22 02:10

akashzincle