管理活动 - Android
Managing Activities - Android
我在维护活动时遇到问题。
我有 activity A,当从 Notification 开始 Activity B 时,我应该结束 Activity A,然后开始 Activity B。
换句话说,同一时间,在堆栈中,应该只有 Activity A 或 Activity B 存在。当我从 Activity A 开始 Activity B 时,通过在 Activity A 中调用 finish()
很容易实现。但是当从 activity 从通知。
堆栈应该是这样的,
- 主要Activity -- Activity A
- MainActivity -- Activity B(从通知启动 activity B 时)
但是我的堆栈看起来像,
- 主要Activity -- Activity A -- Activity B
为此应该使用什么 Intent 标志或启动模式。
使用您的通知发送 PendingIntent
到 Activity A,在 onNewIntent()
中处理并开始 Activity B,完成 Activity A正如你所描述的。
你为什么不在两个活动的 onStop()
中调用 finish()
,
以这种方式,您将始终有一个 activity 在它的下方,一旦您离开这些 activity 它们就会关闭,您将丢失该数据,
根据你目前的要求,我认为只有一个 Activity
应该存在,所以这可能会解决你的问题
您可以尝试设置
android:launchMode="singleTask"
在您声明活动的清单中。
If this attribute is set to "true" in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it.
我在维护活动时遇到问题。
我有 activity A,当从 Notification 开始 Activity B 时,我应该结束 Activity A,然后开始 Activity B。
换句话说,同一时间,在堆栈中,应该只有 Activity A 或 Activity B 存在。当我从 Activity A 开始 Activity B 时,通过在 Activity A 中调用 finish()
很容易实现。但是当从 activity 从通知。
堆栈应该是这样的,
- 主要Activity -- Activity A
- MainActivity -- Activity B(从通知启动 activity B 时)
但是我的堆栈看起来像,
- 主要Activity -- Activity A -- Activity B
为此应该使用什么 Intent 标志或启动模式。
使用您的通知发送 PendingIntent
到 Activity A,在 onNewIntent()
中处理并开始 Activity B,完成 Activity A正如你所描述的。
你为什么不在两个活动的 onStop()
中调用 finish()
,
以这种方式,您将始终有一个 activity 在它的下方,一旦您离开这些 activity 它们就会关闭,您将丢失该数据,
根据你目前的要求,我认为只有一个 Activity
应该存在,所以这可能会解决你的问题
您可以尝试设置
android:launchMode="singleTask"
在您声明活动的清单中。
If this attribute is set to "true" in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it.