Search code examples
androidandroid-intentdeep-linkingintentfilter

How to make multiple Deep Linking in two different activity without duplicate the app android


I am using deep linking to share my active link to different applications like WhatsApp. The problem is I want to share 2 different activities. Now I am able to share them but if we assume I will share activity A. After clicking on the link, I will see my application option well that's fine and it will take me to activity A.

But now if I do share to activity B.When I try to click on the link, my application will appear twice at one time, and if I choose what was previously chosen by activity A, it will take me to my activity A.This is a wrong choice, so the requested activity will not work.

See the pictures for clarification this is activity A:

enter image description here

And this is activity B here problem :

enter image description here

As you can see my app come at two time.

So what is the problem are there anyone know solve to this problem help me.

this is manifest code:


<!--   1-->
        <activity
            android:name=".FragmanM.MainActivityM" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </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:scheme="http"
                    android:host="============"
                    android:pathPrefix="/post" />
            </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:scheme="https"
                    android:host="==============="
                    android:pathPrefix="/post" />
            </intent-filter>
        </activity>

<!--    2   -->

        <activity
            android:name=".FragmantA.MainActivityA" >
            <intent-filter >

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </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:scheme="http"
                    android:host="================"
                    android:pathPrefix="/posts" />
            </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:scheme="https"
                    android:host="==============="
                    android:pathPrefix="/posts" />
            </intent-filter>
        </activity>

this activity A


Uri data =getActivity(). getIntent().getData();

        if (data!= null) {
            try {
                post_id =  data.getLastPathSegment().toString();
                getPost(post_id);


            } catch (NumberFormatException e) {
                post_id=null;
            }



        }


        Bundle bundle = getActivity().getIntent().getExtras();
        if (bundle !=null){
            if(post_id==null){
                post_id =bundle.getString("mid");


                getPost(post_id);


            }

        }


this is activity B



Uri data =getActivity(). getIntent().getData();

        if (data!= null) {
            try {
                posts_id =  data.getLastPathSegment().toString();
                getPost(posts_id);



            } catch (NumberFormatException e) {
                posts_id=null;
            }



        }


        Bundle bundle = getActivity().getIntent().getExtras();
        if (bundle !=null){
            if(posts_id==null){
                posts_id =bundle.getString("moid");
                getPost(posts_id);


            }

        }


Solution

  • Both of your activities are providing intent filters but the reason for this is, you are providing a similar path prefix.

    What I mean by this is in your first activity i.e .FragmanM.MainActivityM you have mentioned

     <data
          android:scheme="http"
          android:host="============"
          android:pathPrefix="/post" /> 
    

    And in .FragmantA.MainActivityA you have written this

     <data
          android:scheme="http"
          android:host="============"
          android:pathPrefix="/posts" /> 
    

    Now take a look at the pathPrefix

    By the definition and according to documentation pathPrefix :

    The pathPrefix attribute specifies a partial path that is matched against only the initial part of the path in the Intent object

    So when you come across links such as this: www.yourhost.com/posts the first activity also gets shown and the second one also showed which is expected.

    So how to fix this?

    Method 1: You can remove intent-filter from your second activity and have single activity handle both paths in this case .FragmanM.MainActivityM

    and inside that activity make a check-in onCreate() somewhat like this

            Uri data = getIntent().getData();
            if(data.getPath().startsWith("/posts"))
            {
                //Start Your second activity here 
            }
    

    Method 2: Create a whole new activity just to handle links and from there you filter links and move the user to different screens