How to finish activities up to one specific activity?

时间:2015-11-12 11:08:22

标签: android android-activity back activity-finish

For example, I opened activities A, B, C, D.

I want to finish D and C and return back to B.

I don't want to open activity B with clear tasks and new task flags. I want to keep activity A too so user can return from B to A with back button.

How can I achieve this?

3 个答案:

答案 0 :(得分:4)

Intent intent = new Intent(getApplicationContext(), B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

And in B activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

答案 1 :(得分:0)

I think you can add this.finish() in your activity method onDestroy() in C and D classes, then if you go from C to D, Android will finish this Activity and you will go directly from D to B, instead of D to C.

I hope it will help you.

答案 2 :(得分:-1)

Make a global static boolean say closeActivity. Set it to false by default. When D is ended set the variable to true. In onResume of activity C check if variable is set to true and if true, call finish(). Do this until activity B is reached and in onResume of B set the boolean to false