android - 当我通过后退按钮关闭应用程序并从任务重新启动时, Intent 数据仍然存在

标签 android android-intent

我开发了一个深层链接应用程序。 当我通过后退按钮关闭应用程序并从深层链接启动后从任务重新启动时, Intent 数据仍然存在。

list :

<application
    <activity
        android:name=".MainActivity"
        android:label="@string/appName"
        android:launchMode="singleTask">
        
        <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="news"
                android:scheme="example" />

        </intent-filter>
    </activity>

    <activity
        android:name=".NextActivity" />

</application>

主要 Activity :

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (intent.action == Intent.ACTION_VIEW && Uri.parse(intent.data.toString()).host == "news") {
            // Transition NextActivity
        }
    }
}

如果用后退按钮关闭后启动,“Transition Next Activity”的代码会被多次传递。

我尝试过,但没有成功。

intent.data = null
setIntent(intent)

最佳答案

我在 Intent 中回答了有关“额外”的类似问题在 Notification 。问题是一样的。当您第一次启动应用程序时(在您的情况下通过深层链接),Android 会记住 Intent用于启动应用程序。当您的用户从“最近任务”列表返回到应用程序时,Android 使用相同的(记住的)Intent 启动应用程序。 。要解决这个问题,您有两个选择:

  1. 添加Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS在你的深层链接中。这将阻止该任务显示在最近任务列表中,从而显示深层链接 Intent以免被记住。

  2. 检测 Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 。当用户从最近任务列表中启动应用程序时,Android 会将此标志添加到 Intent传递给您的 ActivityonCreate() 。您可以检查该标志是否存在,如果设置了,则可以清除Intent中的数据。以便您的应用不会执行与通过深层链接启动时相同的操作。

请参阅我的回答以获取更多信息:https://stackoverflow.com/a/19820057/769265

关于android - 当我通过后退按钮关闭应用程序并从任务重新启动时, Intent 数据仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66490360/

相关文章:

安卓 : deleting row from custom Listview on button click

java - 如何在 Adob​​e Reader 中从 Android 应用程序打开服务器上的 pdf

android - 正确使用 Intent 标志

android-intent - Xamarin 上自定义文件扩展名的意图过滤器实现

java - 如何使用自动完成 TextView

android - Android源码编译Helloworld的方法

Android:如何将字节数组转换为位图?

android - 应用程序被强制停止/终止后,Widget Pending Intent 将不起作用

安卓 : No Activity found to handle Intent

java - 一旦计时器用完,如何使用 Intent 将字符串传递给主要 Activity ?