如何在两个不同的 activity 中创建多个深度链接而不重复应用程序 android

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

我正在使用深度 linking 将我的活动 link 分享给不同的应用程序,例如 WhatsApp。 问题是我想分享 2 个不同的活动。 现在我可以分享它们了,但如果我们假设我会分享 activity A. 单击 link 后,我会看到我的应用程序选项很好,它会带我到 activity A.

但是现在如果我分享到 activity B.When 我尝试点击 link,我的应用程序将一次出现两次,如果我选择之前的chosen by activity A, it will take me to my activity A.This 是一个错误的选择,所以请求的 activity 将不起作用。

请查看图片以了解这是 activity 答:

这里是activity B 问题:

如您所见,我的应用分两次出现。

这是什么问题,有知道解决这个问题的人帮帮我吗。

这是清单代码:


<!--   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>

这个activity一个


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);


            }

        }


这是activityB



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);


            }

        }

您的两个活动都提供了意图过滤器,但这是因为您提供了相似的路径前缀。

我的意思是在您的第一个 activity 中,即您提到的 .FragmanM.MainActivityM

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

并且在 .FragmantA.MainActivityA 你已经写了这个

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

现在看看 pathPrefix

根据定义和文档路径前缀:

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

因此,当您遇到这样的链接时:www.yourhost.com/posts 第一个 activity 也会显示,第二个也会显示,这是预期的。

那么如何解决这个问题?

方法 1:您可以从第二个 activity 中删除 intent-filter,并在这种情况下让单个 activity 处理两个路径 .FragmanM.MainActivityM

然后在里面 activity 签到 onCreate() 有点像这样

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

方法 2:创建一个全新的 activity 来处理链接,然后从那里过滤链接并将用户移动到不同的屏幕