在Android Facebook的深层链接 [英] Facebook deep linking on Android

查看:225
本文介绍了在Android Facebook的深层链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的应用程序Facebook的深层链接功能,并遇到以下情形:

I'm trying to implement Facebook's Deep Linking feature on my app and encountered the following scenario:

我有一个名为MainActivity活动是像这样声明的:

I have an activity called MainActivity which is declared like so:

    <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开发者网站我的应用程序的设置声明。

This activity + my package name are also declared in my app's settings on facebook developer website.

一旦链接被点击Facebook上的应用程序,我应该通过我的活动的onCreate方法来处理此事件。 下面code处理该事件:

Once a link gets clicked on Facebook's app, I'm supposed to handle this event via the onCreate method of my activity. The following code handle the event:

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

一切按计划除以下情形云:

All goes according to plan except for the following scenario:

  1. 用户推出我的应用程序
  2. MainActivity创建
  3. SecondaryActivity创建
  4. MainActivity完成
  5. 应用通过设备主页按钮变为背景
  6. 深层链接被点击Facebook上的应用程序

在这种情况下,我的应用程序去再次前景,但MainActivity的onCreate / onNewIntent 不被调用,而不是SecondaryActivity的onResume()被调用,并恢复到它的 最后的状态。

In this case my app goes to foreground again, but MainActivity's onCreate / onNewIntent don't get called, instead SecondaryActivity's onResume() gets called and restored to it's last state.

注:我已经在三星Nexus采用Android 4.2.1测试了这个问题,并得到了这样的结果,虽然当了Android上的Galaxy S1测试2.3.5它的工作,因为我最初的预期。

Note: I've tested this issue on a Samsung Nexus with Android 4.2.1 and got to this result, though when tested on Galaxy S1 with Android 2.3.5 it worked as I initially expected.

任何帮助将是很大的AP preciated, 谢谢你。

Any help would be greatly appreciated, Thank you.

推荐答案

Facebook正在通​​过显式启动MainActivity开始从自己的应用程序,您的应用程序(一个你提供他们在开发页)。

Facebook is starting your app from their own app by explicitly start your "MainActivity" (the one your provided them in the developer page).

由 - Android的默认行为是:如果应用程序已经运行,然后再次打电话给 startActivity()不会从头开始新的任务,但只有恢复到前台已经在运行的任务。

by that - Android's default behavior is: if the application already runs, then calling again to startActivity() won't start new task from scratch, but only restore to foreground the already running task.

但好消息是,你可以通过添加到您MainActivity的安卓更改此默认行为:launchMode =singleTask。它的定义是:

but the good news are that you can change this default behavior by adding to your MainActivity the android:launchMode="singleTask". it definition is:

系统会创建一个新的任务和实例化活动的新任务的根本。但是,如果活动的实例中已存在一个单独的任务,该系统路由通过调用其onNewIntent()方法,而不是创建新实例意图的现有实例。只有活动的一个实​​例可以在一个时间存在。

the system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

从这个角度,你可以总是回复到起始的意图,从这一点,你可以随时通过重新启动活动有两个标志意图返回到该已在后台(如果存在)的任务。 FLAG_ACTIVITY_SINGLE_TOP &功放;&安培; Intent.FLAG_ACTIVITY_CLEAR_TOP 组合

from this point you could always respond to the starting intent, and from that point you can always navigate back to the task that already was in background(if exists) by restarting activity with both flags Intent.FLAG_ACTIVITY_SINGLE_TOP && Intent.FLAG_ACTIVITY_CLEAR_TOP combination

这篇关于在Android Facebook的深层链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆