Android activity launch mode and deeplink issue
Asked Answered
B

3

9

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" />
Broughton answered 3/3, 2016 at 15:55 Comment(2)
Maybe try to create another activity, something like DeepLinkActivity, set it to handle deep links, then fire the right activity from there using intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)Plicate
@Plicate thanks for the tip. It actually led me to find the answer to my problem! See my answer bellow for more detailsBroughton
B
16

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.

Broughton answered 7/3, 2016 at 7:7 Comment(3)
We had the same problem and also used this flag. The only disadvantage is that if another activity was opened in app, this activity stays in history (below newly opened / reordered one). We couldn't use CLEAR_TASK | NEW_TASK, because it affects, whether app is shown in recents. So we decided, that we can live with another activity in history, because such case is very rare for us.Adze
Nice combination.Nall
i was also looking for the same solution. Thanks!Intercede
R
0
allowTaskReparenting = true

https://developer.android.com/guide/topics/manifest/activity-element#reparent

May have solved your issue (with this set, when you return to the forwarding app, you will be returning to the activity natural for that app and not the activity that it launched in the target app) Not too sure why true isn't the default for this...

Radloff answered 16/12, 2019 at 11:13 Comment(0)
C
0

I had quite the same problem, but on my Application, I never want to return to Login Activity, because the session must stay active due to business rules. So I resolved to create a new AppLinkActivity which started the MainActivity. But MainActivity is set launchMode=singleInstance on Manifest, and started with the following flags:

Intent intent = new Intent(AppLinkActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Coriecorilla answered 31/8, 2021 at 17:29 Comment(1)
this flag FLAG_ACTIVITY_REORDER_TO_FRONT will be ignored This flag will be ignored if {@link #FLAG_ACTIVITY_CLEAR_TOP} is also specified. cs.android.com/android/platform/superproject/+/…Advisedly

© 2022 - 2024 — McMap. All rights reserved.