In Android I have some activities, let's say A, B, C.
In A I use this code to open B:
Intent intent = new Intent(this, B.class);
startActivity(intent);
In B I use this code to open C:
Intent intent = new Intent(this, C.class);
startActivity(intent);
When the user taps a button in C I want to go back to A and clear the back stack (close both B and C). So when the user use the back button B and C will not show up, I've been trying the following:
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
But B and C are still showing up if I use the back button when I'm back in activity A. How can I avoid this?
I found an interesting solution which might help. I did this in my
onBackPressed()
method.FinishAffinity
removes the connection of the existing activity to its stack. And then finish helps you exit that activity. Which will eventually exit the application.The given code works correctly. I have tried in the Application Life Cycle sample.
I haven't got B and C in the back stack after starting activity A with flag, FLAG_ACTIVITY_CLEAR_TOP
This worked for me with onBackPressed:
It sounds to me like you need to start
Activity C
fromActivity B
by usingstartActivityForResult()
. When you click a button inActivity C
, callsetResult(RESULT_OK)
andfinish()
soActivity C
is ended. InActivity B
, you could have theonActivityResult()
respond by also callingfinish()
on itself, and you'd then be taken back toActivity A
.