我正在尝试在我的应用上实施Facebook的深层链接功能,并遇到以下情况:
我有一个名为MainActivity的活动,声明如下:
<activity
android:name="com.mypackage.android.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
此活动+我的包名也在我的应用程序的Facebook开发者网站设置中声明。
在Facebook的应用程序上点击链接后,我应该通过我的活动的onCreate方法处理此事件。 以下代码处理事件:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri target = getIntent().getData();
if (target != null){
// got here via Facebook deep link
// once I'm done parsing the URI and deciding
// which part of my app I should point the client to
// I fire an intent for a new activity and
// call finish() the current activity (MainActivity)
}else{
// activity was created in a normal fashion
}
}
除了以下情况外,一切按计划进行:
在这种情况下,我的应用程序再次进入前台,但MainActivity的onCreate / onNewIntent 不要调用,而是调用SecondaryActivity的onResume()并将其恢复到它 最后一个州。
注意:我在使用Android 4.2.1的Samsung Nexus上测试了这个问题并得到了这个结果,尽管在使用Android 2.3.5的Galaxy S1上进行测试时,它的工作方式与我最初预期的一样。
任何帮助将不胜感激, 谢谢。
答案 0 :(得分:9)
Facebook通过明确启动您的“MainActivity”(您在开发者页面中提供的那个)从他们自己的应用启动您的应用。
由此 - Android的默认行为是:如果应用程序已经运行,则再次调用startActivity()
将无法从头开始执行新任务,但仅恢复到已经运行的任务的前台。强>
但好消息是,您可以通过将android:launchMode="singleTask"
添加到MainActivity来更改此默认行为。它的定义是:
系统创建新任务并在新任务的根目录下实例化活动。但是,如果活动的实例已存在于单独的任务中,则系统会通过调用其onNewIntent()方法将意图路由到现有实例,而不是创建新实例。一次只能存在一个活动实例。
从这一点开始,您始终可以响应起始意图,从那时起,您可以通过使用标志Intent.FLAG_ACTIVITY_SINGLE_TOP
&amp;&amp;重新启动活动,始终导航回已经在后台的任务(如果存在) ; Intent.FLAG_ACTIVITY_CLEAR_TOP
组合
答案 1 :(得分:1)
请参阅http://developer.android.com/guide/topics/manifest/activity-element.html
你可以玩:
android:clearTaskOnLaunch
android:noHistory
android:launchMode
答案 2 :(得分:0)
您需要在意图过滤器中获得更多信息:
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:host="www.yoursite.com" android:scheme="http"></data>
</intent-filter>
这将捕获进入您网站的链接(确保更改网址),并将其定向到您在此下定义此意图过滤器的任何活动。