如何在两个不同的活动中进行多个深层链接而不重复应用程序android [英] How to make multiple Deep Linking in two different activity without duplicate the app android
问题描述
我正在使用深层链接将我的活动链接共享给不同的应用程序,如WhatsApp. 问题是我想分享2个不同的活动. 现在,我可以共享它们了,但是如果我们假设我将共享活动A. 单击链接后,我会很好地看到我的应用程序选项,它将带我进入活动A.
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.
但是现在,如果我确实共享活动B.当我尝试单击链接时,我的应用程序将一次出现两次,如果我选择了活动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.
查看图片以确认这是活动A:
See the pictures for clarification this is activity A:
这是活动B在这里出现的问题:
And this is activity B here problem :
您可以看到我的应用程序两次出现.
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.
这是清单代码:
<!-- 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);
}
}
推荐答案
您的两个活动都提供了意图过滤器,但是这样做的原因是,您提供了类似的路径前缀.
Both of your activities are providing intent filters but the reason for this is, you are providing a similar path prefix.
我的意思是,这是您的第一次活动,即您提到的.FragmanM.MainActivityM
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" />
在.FragmantA.MainActivityA
中,您已经写了这个
<data
android:scheme="http"
android:host="============"
android:pathPrefix="/posts" />
现在来看一下pathPrefix
根据定义并根据文档pathPrefix:
By the definition and according to documentation pathPrefix :
pathPrefix
属性指定匹配的部分路径 仅针对Intent对象中路径的初始部分
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 第一个活动也会显示出来,第二个活动也会显示出来.
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.
那该如何解决呢?
方法1:在这种情况下,您可以从第二个活动中删除意图过滤器,并让单个活动处理两条路径.FragmanM.MainActivityM
Method 1: You can remove intent-filter from your second activity and have single activity handle both paths in this case .FragmanM.MainActivityM
在该活动中进行这样的签入onCreate()
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
}
方法2:创建一个全新的活动,仅用于处理链接,然后从此处过滤链接并将用户移动到其他屏幕
Method 2: Create a whole new activity just to handle links and from there you filter links and move the user to different screens
这篇关于如何在两个不同的活动中进行多个深层链接而不重复应用程序android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!