首页 > 解决方案 > 如何在两个不同的活动中进行多个深度链接而不重复应用程序android

问题描述

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

但是现在如果我确实分享到活动 B。当我尝试单击链接时,我的应用程序将一次出现两次,如果我选择活动 A 之前选择的内容,它将带我到我的活动 A。这个是一个错误的选择,因此请求的活动将不起作用。

请参阅图片以进行说明,这是活动 A:

在此处输入图像描述

这是活动 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>

本次活动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);


            }

        }


这是活动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);


            }

        }

标签: androidandroid-intentdeep-linkingintentfilter

解决方案


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

我的意思是在你的第一个活动中,即.FragmanM.MainActivityM你提到的

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

.FragmantA.MainActivityA写了这个

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

现在看看pathPrefix

根据定义并根据文档 pathPrefix :

pathPrefix属性指定仅与 Intent 对象中路径的初始部分匹配的部分路径

因此,当您遇到诸如www.yourhost.com/posts之类的链接时,第一个活动也会显示出来,而第二个活动也会显示出来,这是预期的。

那么如何解决这个问题呢?

方法 1:您可以从第二个活动中删除意图过滤器,并在这种情况下让单个活动处理两个路径.FragmanM.MainActivityM

并在该活动中进行签到,onCreate()有点像这样

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

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


推荐阅读