如何在 intent-filter 启动 activity 时关闭以前的活动?
How to close previous activities when activity launch by intent-filter happens?
我有一个 Activity,通过这个 intent-flter 从浏览器启动:
<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="myapp" />
<data android:host="someaction"/>
<data android:path="/" />
</intent-filter>
但是如果我的应用程序的其他活动没有被销毁,它们将出现在启动后activity。我需要以某种方式使用 CLEAR_TOP 标志但带有意图过滤器。 Activity 的启动模式设置为 singleTop
而且我不能使用 singleInstance 或 singleTask。那么如何才能只启动我想要的 activity 而不是其他人呢?
解决方案 1
我会在一些 BroadCast
事件中尝试这个。从您开始的 Activity
发送一个,然后将 BroadcastReceivers
添加到您想要关闭的那些。
勾选this answer,我想应该对你有帮助。
解决方案 2
我也想试试这个:将你 activity 的意图标志设置为 FLAG_ACTIVITY_CLEAR_TOP
。您还可以考虑使用以下意图标志:FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
我有一个 Activity,通过这个 intent-flter 从浏览器启动:
<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="myapp" />
<data android:host="someaction"/>
<data android:path="/" />
</intent-filter>
但是如果我的应用程序的其他活动没有被销毁,它们将出现在启动后activity。我需要以某种方式使用 CLEAR_TOP 标志但带有意图过滤器。 Activity 的启动模式设置为 singleTop
而且我不能使用 singleInstance 或 singleTask。那么如何才能只启动我想要的 activity 而不是其他人呢?
解决方案 1
我会在一些 BroadCast
事件中尝试这个。从您开始的 Activity
发送一个,然后将 BroadcastReceivers
添加到您想要关闭的那些。
勾选this answer,我想应该对你有帮助。
解决方案 2
我也想试试这个:将你 activity 的意图标志设置为 FLAG_ACTIVITY_CLEAR_TOP
。您还可以考虑使用以下意图标志:FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK