简体   繁体   English

完成多项活动以恢复堆栈中的旧活动?

[英]Finishing multiple activities to resume an old one in stack?

I need to finish multiple activities in stack and resume an old one.我需要在堆栈中完成多项活动并恢复旧活动。 For example,例如,

There are four activities in stack Act1, Act2, Act3 and Act4.堆栈 Act1、Act2、Act3 和 Act4 中有四个活动。 On some event in Act4, I need to finish Act4, Act3 and Act2 to resume Act1.在 Act4 中的某些事件中,我需要完成 Act4、Act3 和 Act2 以恢复 Act1。

I tried using Intet.FLAG_ACTIVITY_CLEAR_TOP but it's recreating the activity which is not my requirement.我尝试使用 Intet.FLAG_ACTIVITY_CLEAR_TOP 但它正在重新创建不是我要求的活动。

Advance thanks for suggestions.提前感谢您的建议。

You have used Clear_top, it will clear all the activities and bring act1 to front, but, onNewIntent method will be called of you activity, you can perform any task you want in that, after activity starts again.您已经使用了 Clear_top,它将清除所有活动并将 act1 带到前面,但是,您的活动将调用 onNewIntent 方法,您可以在活动再次启动后执行您想要的任何任务。 Or, you can use flag FLAG_ACTIVITY_REORDER_TO_FRONT with your intent, it will do:或者,您可以根据自己的意图使用标志 FLAG_ACTIVITY_REORDER_TO_FRONT,它将执行以下操作:

FLAG_ACTIVITY_REORDER_TO_FRONT do this:If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running. FLAG_ACTIVITY_REORDER_TO_FRONT 这样做:如果在传递给 Context.startActivity() 的 Intent 中设置,这个标志将导致启动的活动被带到其任务历史堆栈的前面,如果它已经在运行。

FLAG_ACTIVITY_CLEAR_TOP will do: If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. FLAG_ACTIVITY_CLEAR_TOP 会做:如果设置,并且正在启动的活动已经在当前任务中运行,那么不是启动该活动的新实例,而是关闭它上面的所有其他活动,并将此 Intent 传递给(现在在顶部)旧活动作为新意图。

for more information on flags look http://developer.android.com/reference/android/content/Intent.html有关标志的更多信息,请查看http://developer.android.com/reference/android/content/Intent.html

There are four activities in stack Act1, Act2, Act3 and Act4.堆栈 Act1、Act2、Act3 和 Act4 中有四个活动。 On some event in Act4, I need to finish Act4, Act3 and Act2 to resume Act1.在 Act4 中的某些事件中,我需要完成 Act4、Act3 和 Act2 以恢复 Act1。

Do this:做这个:

Intent intent =  new Intent(Act4.this, Act1.class);      
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

use android:launchMode,will be ok.使用 android:launchMode,就可以了。

<Act1
...
android:launchMode="singleTask">

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

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