我开发了一个深层链接应用。 当我通过后退按钮关闭应用程序并从深层链接启动后从任务重新启动时,意图数据仍然存在。
清单:
<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>
主要活动:
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)
答案 0 :(得分:0)
我在 Intent
中的 Notification
中回答了一个关于“额外”的类似问题。问题是一样的。当您第一次启动应用程序时(在您的情况下是通过深层链接),Android 会记住用于启动应用程序的 Intent
。当您的用户从“最近的任务”列表返回应用程序时,Android 使用相同的(记住的)Intent
启动应用程序。要解决这个问题,您有 2 个选择:
在您的深层链接中添加 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
。这将阻止任务显示在最近任务列表中,从而防止记住深层链接 Intent
。
检测 Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
。当用户从最近任务列表中启动应用程序时,Android 会将此标志添加到传递给您的 Intent
的 Activity
的 onCreate()
。您可以检查此标志是否存在,如果已设置,您可以清除 Intent
中的数据,以便您的应用不会执行与通过深层链接启动时相同的操作。
请在此处查看我的回答以了解更多信息:https://stackoverflow.com/a/19820057/769265