Figen Güngör
Figen Güngör

Reputation: 12559

How to finish activities up to one specific activity?

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?

Upvotes: 4

Views: 2519

Answers (3)

SKT
SKT

Reputation: 1851

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

Upvotes: -1

Samuel
Samuel

Reputation: 1

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.

Upvotes: 0

Kamajabu
Kamajabu

Reputation: 506

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);
}

Upvotes: 5

Related Questions