8
votes

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.

1
Use this android:launchMode="singleInstance" instead of android:launchMode="singleTask". Hope it will work. - Vishal Chhodwani
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 singleInstance - Nouman Bhatti
post your entire manifest - David Wasser
@DavidWasser I have updated Manifest file. SignIn and Signup activities are opening from MainActivity - Nouman Bhatti
@NoumanBhatti your question helped me big time. I was not aware of onNewIntent() handle. - rizzz86

1 Answers

2
votes

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.