'App always launches at main activity from launcher despite alwaysRetainTaskState in singleTask mode
I have a launcher Activity declared like so:
<activity
android:name=".MainActivity"
android:exported="true"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize"
android:theme="@style/Theme.MyApplication.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In this activity I have a Fragment and inside the fragment in response to a user action I launch another Activity like so:
(this.context as? Activity)?.let { ctx ->
ctx.startActivity(Intent(ctx, SomeOtherActivity::class.java))
}
The second activity is also declared as singleTask like so:
<activity
android:name=".ui.SomeOtherActivity"
android:exported="false"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize" />
This adds the new activity correctly to the back stack so the user can use back OS button to go back to MainActivity no problem.
Now if I open SomeOtherActivity, go to home screen and launch my app again, I see MainActivity instead.
According to some other answers, setting android:alwaysRetainTaskState should be a solution but that didn't work.
How can I get the app to show the last activity user was on without manually having to store that in the Shared Preferences?
For me the flow involves quite a few activities in a liner fashion (e.g. user picks something from one list, goes to that activity which shows another list, the selection there is another activity and so on..)
If I remove android:launchMode="singleTask" from both activities then I do end up going back to SomeOtherActivity.
However, the way my application is designed once user selects something in the first activity the context for underlying singleton classes switches so I want to only ever have one instance of ALL my activities to avoid the UI thinking the user chose A when the underlying singleton and data persistence thinks it's B (because that somehow came in another instance of activity).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
