Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 639
Nouman Bhatti Avatar asked Dec 09 '16 07:12

Nouman Bhatti


People also ask

What is Android deep linking?

A deep link is a URL that navigates to a specific destination in your app. When you click a deep link, Android: Opens the user's preferred app that can handle the link, if it's available. If the preferred app isn't available, it opens the only app that can handle the link.

Can you deep link to another app?

Passing search data via deep linking Developers will need to submit their app and deep linking apps on both iOS and Android to be indexed by Google. Alternatively, developers can use Google's short links to deep link mobile app users if the app is installed and direct others to the webpage.

What is the purpose of deep linking?

What is deep linking? Deep links are a type of link that send users directly to an app instead of a website or a store. They are used to send users straight to specific in-app locations, saving users the time and energy locating a particular page themselves – significantly improving the user experience.

What is Android Launchmode?

This is the default launch mode of activity (If not specified). It launches a new instance of an activity in the task from which it was launched. Numerous instances of the activity can be generated, and multiple instances of the activity can be assigned to the same or separate tasks.

What is a deep link in Android?

In Android, a deep link is a link that takes you directly to a specific destination within an app. The Navigation component lets you create two different types of deep links: explicit and implicit. An explicit deep link is a single instance of a deep link that uses a PendingIntent to take users to a specific location within your app.

What is the difference between singletask and singleinstance activity?

This mode is quite close to singleTask, only single instance of Activity could be existed in the system. The difference is Task hold this Activity could have only one Activity, the singleInstance one. If another Activity is called from this kind of Activity, a new Task would be automatically created to place that new Activity.

How do I create a deep link in Android navcontroller?

val componentName = ... If you have an existing NavController , you can also create a deep link by using NavController.createDeepLink (). An implicit deep link refers to a specific destination in an app. When the deep link is invoked—for example, when a user clicks a link—Android can then open your app to the corresponding destination.

How to launch a specific activity once a user clicks on Deeplink?

Depending on your requirement you can launch the specific Activity once a user clicks on the deep link. It is important to note that once a user clicks on a deep link, the Android Activity will launch on top of the current visible Activity (In case if app is open).


1 Answers

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.

like image 91
David Wasser Avatar answered Oct 20 '22 19:10

David Wasser