Android Deep linking and singleInstance/singleTask
Asked Answered
E

1

9

Possible Duplicate Deep linking and multiple app instances. I have implemented Deep Linking in my app. I have Splash activity that is launcher and MainActivity that handles the Intent as defined in manifest:

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:fullBackupContent="true"
    android:icon="@drawable/app_logo"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".ActivitySplash"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name">
        <intent-filter>
            <!-- Launcher activity -->
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ActivityMain"
        android:alwaysRetainTaskState="true"
        android:configChanges="orientation|screenSize"
        android:exported="true"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize">
        <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="www.mywebsite.com"
              android:pathPrefix="/something"
              android:scheme="http" />
        </intent-filter>
    </activity>
   <activity
        android:name=".ActivitySignIn"
        android:configChanges="screenSize|orientation" />
   <activity android:name=".ActivitySignUp" />
</application>

I have set launch mode singleTask to handle onNewIntent(). Now what i want to achieve is that if user opens activity from DeepLinking and there is already some task going on in MainActivity I prompt user a dialog either he want to cancel current task and start new task (which is from deep linking). The issue is If i open another activity from MainActivity and user comes from DeepLinking Intent. Then it would kill the second activity and directly open MainActivity. What i want to achieve is that if app/activity is not running then Intent from DeepLink open as is. And if activity/app is already running then i prompt user to either close current task and perform DeepLink task/intent.

Esau answered 9/12, 2016 at 7:15 Comment(5)
Use this android:launchMode="singleInstance" instead of android:launchMode="singleTask". Hope it will work.Gloom
I was using singleInstance before and in that case if i resume app and open again by tapping app icon it will first show Splash instead of showing last state. And also i do get a weird animation in all activity loading by using singleInstanceEsau
post your entire manifestParmenter
@DavidWasser I have updated Manifest file. SignIn and Signup activities are opening from MainActivityEsau
@NoumanBhatti your question helped me big time. I was not aware of onNewIntent() handle.Roundel
P
5

This doesn't really work the way you think it does. You are trying to use launchMode="singleTask", but since you haven't also set "taskAffinity", Android pretty much ignores your launchMode.

You should not need to use either of the special launch modes "singleTask" or "singleInstance" to get what you want.

Try using singleTop launch mode and see if this solves your problem. If ActivityMain is already open and you launch ActivityMain again using your deep-link, onNewIntent() should be called in ActivityMain.

You can also look at my answer to this question which describes a way to determine what Activity to show based on using a static variable to decide whether another Activity is in the stack or not.

Parmenter answered 9/12, 2016 at 12:10 Comment(8)
In singleTop case, What if SignIn activity is opened which is child activity of ActivityMain. And i open MainActivity from DeepLink, then it opens activity main by creating its new instance in new task and on clicking back it again shows SignIn activity and on one more back press it shows ActivityMain againEsau
Yes. That's true. What would you like to happen in that case?Parmenter
I want to maintain the navigation flow, if flow was ActivityMain>SignIn activity and then user try to start ActivityMain again from DeepLink then i want to prompt user if he want to leave SignIn activity or not, if yes then show ActivityMain with deeplink task state, else show previous keep showing SignIn activity. And when user go back from SignIn to Main activity it should show previous ActivtyMain stateEsau
Basically my main activity has side menu and contains fragments and those fragments have certain state let say listview scroll, and from side menu i can open another activity signin etc. So in singleTop case it will create a new task with new instance of Activitymain losing previous state of ActivitymainEsau
Fine. In this case you should use the mechanism I described in the linked question. Your "deep link" should just start a "dispatcher" Activity. That Activity can then determine what the current state of your app is and redirect or react accordingly.Parmenter
See #40996826Parmenter
and what launch mode should i use for dispatcher activity?Esau
Standard launch mode should be fine.Parmenter

© 2022 - 2024 — McMap. All rights reserved.