android - Android 上的 Facebook 深度链接

标签 android facebook android-intent

我正在尝试在我的应用上实现 Facebook 的深度链接功能并遇到以下情况:

我有一个名为 MainActivity 的 Activity ,它的声明如下:

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

此 Activity + 我的包名称也在我的应用程序设置中声明在 facebook 开发者网站上。

一旦链接在 Facebook 的应用程序上被点击,我应该通过我的 Activity 的 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
        }
    }

一切都按计划进行,除了以下情况:

  1. 用户启动了我的应用
  2. 已创建 MainActivity
  3. SecondaryActivity 已创建
  4. MainActivity 完成
  5. 应用通过设备主页按钮进入后台
  6. Facebook 的应用程序点击深层链接

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

注意:我已经在装有 Android 4.2.1 的三星 Nexus 上测试了这个问题并得到了这个结果,但在装有 Android 2.3.5 的 Galaxy S1 上进行测试时,它按我最初的预期工作。

任何帮助将不胜感激, 谢谢。

最佳答案

Facebook 正在通过显式启动您的“MainActivity”(您在开发者页面中提供给他们的那个)从他们自己的应用启动您的应用。

这样 - Android 的默认行为是:如果应用程序已经运行,那么再次调用 startActivity() 不会从头开始新任务,但 只会恢复到前台已经正在运行的任务。

但好消息是,您可以通过向 MainActivity 添加 android:launchMode="singleTask" 来更改此默认行为。它的定义是:

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.

从此时起,您始终可以响应起始 Intent ,并且从那时起,您始终可以通过使用两个标志 Intent.FLAG_ACTIVITY_SINGLE_TOP && Intent.FLAG_ACTIVITY_CLEAR_TOP 组合

关于android - Android 上的 Facebook 深度链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16195889/

相关文章:

javascript - 示例聊天应用程序中的 Flux 架构误解

android - 我们可以通过 intent 在 android 中传输多少数据?

android - 为什么 Fragments 从它们的 Activity 中分离出来?

android - adb 找不到我的设备/手机 (MacOS X)

java - Lidbgx 从右向左移动对象

安卓 :download file from server unable to decode stream

ios - 转换为 swift 3 语法后,应用无法通过 Facebook 和 Firebase 进行身份验证

css - 将 CSS 添加到 Facebook Like Box

android - 访问联系人过滤器

android - 在分享 Intent android 中使用 Facebook sdk 的 ShareContent