Astronautilus
Astronautilus

Reputation: 83

onBackPressed() should call previous activity's onBackPressed()

So here is the problem, I perform A → B and from B I do B → C , now when the back button is pressed in C I want to go back to A. so at start the flow is A → B → C but I want to clear B when I start C so when back pressed from C I go back to A. I know I can start A from C and CLEAR_TOP but I am looking for another alternative if there is one.

Thank You

Upvotes: 0

Views: 769

Answers (5)

Varma460
Varma460

Reputation: 507

    //FROM A TO B
    Intent A = new Intent(A.this,B.class);
    startActivity(A);

    //FROM B TO C
    Intent B = new Intent(B.this,C.class);
    startActivity(B);
    finish();

    //FROM C TO A
    onBackPressed(); you can go directly to A.

hope this may help u.

Upvotes: 2

ekilic
ekilic

Reputation: 93

Intent intent = new Intent(this, C.class);
startActivity(intent);
finish();

If you call finish() after start C Activity, in B Activity. You can remove B Activity from stack. So if you press back in C, you will see A Activity.

Upvotes: 1

Nithinlal
Nithinlal

Reputation: 5061

You just need to call finish()

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();

Upvotes: 3

Ahmed Eltaher
Ahmed Eltaher

Reputation: 161

you can call finish(); after you start intent on B .

Upvotes: 2

Kapil Rajput
Kapil Rajput

Reputation: 11555

call finish(); after passing Intent on Activity you don't want to open on back press.

Upvotes: 2

Related Questions