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.

Community
  • 1
  • 1
Nouman Bhatti
  • 1,777
  • 4
  • 28
  • 55

1 Answers1

4

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.

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    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 again – Nouman Bhatti Dec 09 '16 at 13:50
  • Yes. That's true. What would you like to happen in that case? – David Wasser Dec 09 '16 at 13:55
  • 1
    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 state – Nouman Bhatti Dec 09 '16 at 14:01
  • 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 Activitymain – Nouman Bhatti Dec 09 '16 at 14:06
  • 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. – David Wasser Dec 09 '16 at 15:54
  • See http://stackoverflow.com/questions/40996326/set-different-activities-for-pending-intent-of-notification/41003678#41003678 – David Wasser Dec 09 '16 at 15:55
  • and what launch mode should i use for dispatcher activity? – Nouman Bhatti Dec 10 '16 at 06:12
  • Standard launch mode should be fine. – David Wasser Dec 10 '16 at 06:54