简体   繁体   English

管理活动 - Android

[英]Managing Activities - Android

I have a problem with maintaining the activities.我在维护活动时遇到问题。

I have activity A, and when starting Activity B from Notification, I should end Activity A, and start Activity B.我有活动 A,从通知启动活动 B 时,我应该结束活动 A,然后启动活动 B。

In other words, At a same time, in stack, Only Activity A or Activity B should be present.换句话说,同时,在堆栈中,应该只存在 Activity A 或 Activity B。 Its' very easy achieve when am starting Activity B from Activity A, by calling finish() in Activity A. But cannot achieve the same when starting activity from notification.当我从活动 A 启动活动 B 时,通过在活动 A 中调用finish()很容易实现。但是从通知启动活动时无法实现相同的目标。

Stack should be like this,堆栈应该是这样的,

  • MainActivity -- Activity A MainActivity -- 活动A
  • MainActivity -- Activity B (When starting activity B from notification) MainActivity -- Activity B(当从通知开始活动 B 时)

But My Stack looks like,但我的堆栈看起来像,

  • MainActivity -- Activity A -- Activity B MainActivity -- 活动 A -- 活动 B

What Intent flags or launch mode should be used for this.应该为此使用什么 Intent 标志或启动模式。

ClearTaskOnLaunch 启动时清除任务

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.如果此属性在任务的根活动中设置为“true”,则每当用户离开任务并返回到该任务时,堆栈将被清除到根活动。

使用您的通知将PendingIntent发送到 Activity A,在onNewIntent()处理它并让它启动 Activity B,按照您的描述完成 Activity A。

why don't you call finish() in onStop() of both activities,为什么不在两个活动的onStop()中调用finish()

in this manner you will always have one activity On, down side of it as soon as you go somewhere apart from these activity they will be closed and you will loss that data,以这种方式,您将始终有一个活动,一旦您转到这些活动之外的某个地方,它们就会被关闭,您将丢失该数据,

looking as your current requirement I think only one Activity should live so this might solve your problem根据您当前的要求,我认为只有一个Activity应该存在,因此这可能会解决您的问题

You could try setting你可以尝试设置

android:launchMode="singleTask"

in your manifest where you declare your activites.在你声明你的活动的清单中。

See documentation of launch modes .请参阅启动模式的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM