Nouman Bhatti
Nouman Bhatti

Reputation: 1837

Android Deep linking and singleInstance/singleTask

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.

Upvotes: 9

Views: 9398

Answers (1)

David Wasser
David Wasser

Reputation: 95568

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.

Upvotes: 5

Related Questions