【问题标题】:onCreate method of activity always called with launchmode "singleTask"活动的 onCreate 方法始终使用启动模式“singleTask”调用
【发布时间】:2018-10-08 12:52:07
【问题描述】:

我在我的应用程序中使用Branch IO,根据他们的documentation,我在我的活动中使用android:launchMode="singleTask"

这是我的 AndroidManifest 的一些代码 sn-p。

        <activity
        android:name=".SplashScreen"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <data
                android:host="open"
                android:scheme="com.package.name" />

            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>

        <!-- Branch App Links (optional) -->
        <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="app.link"
                android:scheme="https" />
            <!-- <data android:scheme="https" android:host="example-alternate.app.link" /> -->
        </intent-filter>
    </activity>

一切正常,除了当我在使用应用程序时按主页按钮并点击应用程序/启动器图标时,会调用启动屏幕活动的 onCreate 方法,这使得应用程序看起来像是从一开始就启动的。但是,如果我在使用应用程序时按下主页按钮并从最近的应用程序中打开它,则不会调用 onCreate 并且一切正常。

当从最近的应用和应用/启动器图标移到前台时,如何使应用行为保持一致?

我尝试删除 singleTask 启动模式,这使得从 App/Launcher 图标和最近的应用程序中完美启动,但是当点击分支 IO 链接时,会创建一个应用程序的新实例。我可以理解,要克服这个问题,只有他们要求将 singleTask 置于启动模式。

我已经在许多使用深层链接的应用程序中检查过这种情况,但它们没有这个问题!

我一定做错了,我看不到。

是否缺少某些东西或实施有误?

【问题讨论】:

  • 您是在 onPause 还是 onStop 上完成了您的应用程序?这只发生在您的应用实例停止时。
  • 不,我在 onPause/onStop 中什么也没做。即使是这样,从最近的应用程序打开时它如何正常工作? @NoBody
  • 您可以在此处添加您的活动代码吗?

标签: android android-activity deep-linking branch.io android-deep-link


【解决方案1】:

解决这个问题的方法是转向单一活动架构。 由于 splash Activity 是 singleTask 的,因此每次从启动器图标将应用程序带到前台时,都会调用它的 onStart 方法。必须在 onStart 方法中编写路由逻辑,当从这里启动另一个活动时,应用程序状态将丢失。 如果存在单个活动,则不会存在此问题。然而,人们将不得不处理令人讨厌的片段 backstacks。这不是一个简单但正确的方法。

为了快速解决问题,可以在活动的 onCreate 方法中使用此代码,并将 launchmode 设置为 singleTask。

        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
           return;
        }

这可能有助于避免不必要地执行 onCreate 中编写的代码。但是,这是推荐的解决方案,因为它不能解决问题的根本原因。

【讨论】:

  • 嘿@mihir-patel,这是来自 Branch.io 的 Vatsal,如果问题已解决,我想跟进。如果没有,请告诉我或随时通过 integrations@branch.io 写信给我们
猜你喜欢
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
相关资源
最近更新 更多