简体   繁体   中英

Android clear Activity back stack

我有一些活动,例如A,B,C,D。如何完成活动D,清除堆栈,以避免返回活动C,然后返回活动B。请不要故意从C开始D以获取结果和C中的onActivityResult,再次调用finish =)'因为已经使用finish()))

You can use the FLAG_ACTIVITY_CLEAR_TOP flag.

See the Android documentation here. It actually uses the exact example you gave:

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.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

Im not sure if that behavior needs on permanent bases or it's conditional. In first case you can add in your D manifest activity declaration following line:

android:parentActivityName="com.example.B"

In second case you can override in activity D - onBackPressed() method and start Activity B from here depends on current situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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