我开发了一个深层链接应用程序。 当我通过后退按钮关闭应用程序并从深层链接启动后从任务重新启动时, 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
启动应用程序。 。要解决这个问题,您有两个选择:
添加
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
在你的深层链接中。这将阻止该任务显示在最近任务列表中,从而显示深层链接Intent
以免被记住。检测
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
。当用户从最近任务列表中启动应用程序时,Android 会将此标志添加到Intent
传递给您的Activity
的onCreate()
。您可以检查该标志是否存在,如果设置了,则可以清除Intent
中的数据。以便您的应用不会执行与通过深层链接启动时相同的操作。
请参阅我的回答以获取更多信息:https://stackoverflow.com/a/19820057/769265
关于android - 当我通过后退按钮关闭应用程序并从任务重新启动时, Intent 数据仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66490360/