My app has 2 activities. The root activity of the app is the MainActivity. If credentials are missing or invalid the user will be redirected to the LoginActivity while finishing the MainActivity.
If i set the launch mode of the main activity to singleTask and I am in an inner fragment of the LoginActivity => minimize the app -> launch the app from the launcher icon => The MainActivity launches (since it is a singleTask activity) and redirects to the LoginActivity but of course to the first fragment in the stack.
The wanted behavior (as is happening when re-launching from recents) is that it would retain the instance of the current activity and display the correct fragment in the stack of the LoginActivity.
This could of course be solved easily by setting the launch mode to singleTop but then a different issue happens! DeepLink trouble!
If my app is open in the background and I click on a link from a browser or email when in singleTop mode - the app will be opened as a subtask of the forwarding app (if you click recents you will see your app opened twice - once as a regular instance that was there before and one enclosed within the browser/email). This of course is tremendously inconvenient and can be solved by - well you know - setting the launch mode to singleTask!
Any help on this issue will be most appreciated.
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="somehost"
android:scheme="myscheme" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:name="LoginActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait" />
Well thanks to the tip by orelzion I managed to solve this issue:
I did create a new DeepLinkActivity which started the MainActivity (which i changhed the launch mode from "singleTask" to "singleTop") with the following flags:
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK
All flags are needed in order to take care of a few issues that came up while trying to fix my problem.
来源:https://stackoverflow.com/questions/35776907/android-activity-launch-mode-and-deeplink-issue