Android:最近开始不正确的 Activity

标签 android android-activity deeplink

我正在开发一个有两种启动方法的安卓应用。 一种是按手机上的应用程序图标的正常方式。 另一种方法是使用来自网站的深层链接。

深层链接还会发送应用程序需要执行某些“操作”的一些数据。但是,这应该只进行一次。 深度链接 Activity 完成后,它会启动主要 Activity 。但是,当我(在设备上)按返回并从最近打开应用程序时,它会再次打开深层链接 Activity 。

我可以从 list 中的最近 Activity 中排除深度链接 Activity 。这也排除了最近应用的 mainactivity,这不应该是这样的。

如何防止深度链接 Activity 从最近的应用程序开始?

我的 list :

   <activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme">
    </activity>

    <activity
        android:name="DeeplinkActivity"
        android:label="@string/app_name"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme">

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="app_name" android:host="test" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:host="test" android:path="/" android:scheme="app_name" />
        </intent-filter>
    </activity>

要切换到 MainActivity,我执行以下操作:

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

编辑: 这篇文章被标记为重复:Android: Starting app from 'recent applications' starts it with the last set of extras used in an intent

但是该帖子是关于同一 Activity 的。我想更改根 Activity ,因此当我从最近启动应用程序时,它不会启动 DeeplinkActivity。是的,作为一种解决方法,我可以检查 Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 标志。但是每次用户从最近启动应用程序时,DeeplinkActivity 都会打开,而不再需要它。

设置和/或清除其他 Intent 值似乎不起作用。 我使用来自 getIntent().getData() 的信息

如果您仍然觉得这是重复的,请解释。

最佳答案

你的问题是DeepLinkActivityMainActivity处于同一个任务中,因此当用户从最近任务列表中选择应用程序时,Android 会将现有任务带到前台,或者如果没有现有任务(其中包含实时/Activity Activity ),则启动Activity那是根Activity在最近的任务中。你无法预测哪个 Activity将是根 Activity , 因为任务可以从 DeepLinkActivity 开始或 MainActivity ,取决于用户首先选择了哪一个。

您确实需要有 2 个单独的任务来执行此操作。一项任务将使您的 DeepLinkActivity在里面,这个任务应该从“最近的任务列表”中排除。另一个任务将有你的 MainActivity

我假设 MainActivity<intent-filter>使用 ACTION=MAIN 和 CATEGORY=LAUNCHER,即使您发布的 list 没有显示这一点。

您的 list 应如下所示:

<activity
    android:name="MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:name="DeeplinkActivity"
    android:label="@string/app_name"
    android:noHistory="true"
    android:screenOrientation="portrait"
    android:taskAffinity=""
    android:excludeFromRecents="true"
    android:theme="@style/AppTheme">

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="app_name" android:host="test" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:host="test" android:path="/" android:scheme="app_name" />
    </intent-filter>
</activity>

你绝对不需要launchMode="singleTask"对于 MainActivity你可能不需要它 DeepLinkActivity要么(这取决于你用这个做什么)。

指定 taskAffinity=""确保 DeepLinkActivity未启动与 MainActivity 相同的任务, 并允许您启动 MainActivity来自 DeepLinkActivity在一个单独的任务中。注意:没有指定 taskAffinity ,即使您指定了 launchMode="singleTask",这两个 Activity 也会在同一个任务中结束。对于他们两个。

指定 excludeFromRecents="true"DeepLinkActivity告诉 Android 任务带有 DeepLinkActivity因为它的根 Activity 不应该出现在最近的任务列表中。

启动时 MainActivity来自 DeepLinkActivity ,你应该这样做:

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

并确保您调用了finish()DeepLinkActivity .

如有必要,您还可以添加 noHistory="true"DeepLinkActivity ,但我认为没有必要。如果用户在 DeepLinkActivity接到一个来电,通话结束后,DeepLinkActivity应该显示。如果指定 noHistory="true" , DeepLinkActivity当用户接受来电时,将立即完成。

如果这很清楚并且对你有用,请告诉我。

关于Android:最近开始不正确的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001499/

相关文章:

android - 背景图像拉伸(stretch)

android - 如何从扩展 BaseAdapter 的类启动 Activity?

android - 一个应用程序有两个独立的 Activity

ios - 如果应用程序已安装在 ios 中,则应用程序无法打开并始终从深层链接重定向到应用程序商店

android - 使用 NavDeepLinkBuilder 构建 fragment 回栈

javascript - 如何使用javascript或html打开手机chrome浏览器

android - 如何为双显示器配置 Android x86(即克隆)?

android - 使用 repo/git 下载后有效更改 android 存储库版本的最佳方法

android - Android 上的 DatePicker 示例存在问题。可能的安卓错误

android - 无法从登录 Activity 转到主要 Activity (带有后台任务)