Pasha Oleynik
Pasha Oleynik

Reputation: 515

Handling deeplinks with multiple activities

I have two different activities in my application.

<activity
    android:name=".ui.login.LoginActivity">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <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="mysite.ua"
            android:pathPattern="/.*/confirm-email-for-app"
            android:scheme="https" />
        <data
            android:host="mysite.ua"
            android:pathPattern="/.*/confirm-email-forgot-pwd-app"
            android:scheme="https" />
    </intent-filter>

</activity>

<activity
    android:name=".ui.main.MainActivity">

    <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="mysite.ua"
            android:scheme="https" />

    </intent-filter>
</activity>

So, what I want to achieve is handling deeplinks with patterns outlined in intent-filter of LoginActivity directly in LoginActivity and all other links that doesn't fit otlined patterns is to open in MainActivity.

For now all links that doesn't fit patterns opens correctly in MainActivity. But when I try to open link https://mysite.ua/ua/confirm-email-for-app system shows dialog with two instances of application. One of them leads to LoginActivity and another leads to MainActivity.

Bug

How can I avoid this?

I can not define all the patth patterns in intent-filter of MainActivity because my goal is to open all the links with outlined host in MainActivity (except of those which fits for LoginActivity patterns).

Upvotes: 0

Views: 1481

Answers (1)

Yahya M
Yahya M

Reputation: 472

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <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="mysite.ua"
                    android:pathPattern="/.*/confirm-email-for-app"
                    android:scheme="https" />
                <data
                    android:host="mysite.ua"
                    android:pathPattern="/.*/confirm-email-forgot-pwd-app"
                    android:scheme="https" />

                <data
                    android:host="mysite.ua"
                    android:scheme="https" />
            </intent-filter>

        </activity>

        <activity
            android:name=".ui.main.MainActivity">
        </activity>

In LoginActivity, get dynamic data in a query parameter and redirect as per your condition.

following is the code to get data passed in Dynamic Link

FirebaseDynamicLinks.getInstance()
            .getDynamicLink(getIntent())
            .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                @Override
                public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                    // Get deep link from result (may be null if no link is found)
                    Uri deepLink = null;
                    if (pendingDynamicLinkData != null) {
                        deepLink = pendingDynamicLinkData.getLink();
                        Log.e(TAG, "onSuccess: check deep link url " + deepLink.toString());

                        String path = deepLink.getPath();
                        code = path.substring(path.lastIndexOf('/') + 1);
                        //check your condition and redirect according to that.
                       
                    }
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "getDynamicLink:onFailure", e);
                }
            });

Upvotes: 2

Related Questions