woo*_*oot 21 android facebook android-intent
我正在尝试在我的应用上实现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>
Run Code Online (Sandbox Code Playgroud)
此活动+我的包名也在我的应用程序的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
}
}
Run Code Online (Sandbox Code Playgroud)
除了以下场景外,所有都按计划进行:
在这种情况下,我的应用程序再次进入前台,但MainActivity的onCreate/onNewIntent不会被调用,而是调用SecondaryActivity的onResume()并恢复到它的最后状态.
注意:我已经在使用Android 4.2.1的Samsung Nexus上测试了这个问题并得到了这个结果,尽管在使用Android 2.3.5的Galaxy S1上进行测试时,它按照我最初的预期工作.
非常感谢任何帮助,谢谢.
Facebook正在通过显式启动您的"MainActivity"(您在开发者页面中提供的那个)从他们自己的应用程序启动您的应用程序.
通过这个 - Android的默认行为是:如果应用程序已经运行,那么再次调用startActivity()
将不会从头开始新任务,而只是恢复到已经运行的任务的前台.
但好消息是你可以通过添加到MainActivity来改变这种默认行为android:launchMode="singleTask"
.它的定义是:
系统创建新任务并在新任务的根目录下实例化活动.但是,如果活动的实例已存在于单独的任务中,则系统会通过调用其onNewIntent()方法将意图路由到现有实例,而不是创建新实例.一次只能存在一个活动实例.
从这一点开始,您总是可以响应起始意图,从那时起,您可以通过使用两个标志Intent.FLAG_ACTIVITY_SINGLE_TOP
&& Intent.FLAG_ACTIVITY_CLEAR_TOP
组合重新启动活动,始终导航回已经在后台的任务(如果存在)
归档时间: |
|
查看次数: |
8855 次 |
最近记录: |