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?
This bothers me for a long time .Finally I worked it out by doing this:
In fragment,use:
In Activity,use(add one more intent flag
Intent.FLAG_ACTIVITY_CLEAR_TASK
compared to fragment):For future research, try this code.
i called
activity_name.this.finish()
after starting new intent and it worked for me.But it won't work for me... I am not suggesting this solution for use but if setting flag won't work for you than you can try this..But still i recommend don't use it
In addition to
FLAG_ACTIVITY_CLEAR_TOP
, you may try addingIntent.FLAG_ACTIVITY_SINGLE_TOP
as well:intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
I tried all solutions and none worked individually for me. My Solution is :
Declare
Activity A
asSingleTop
by using[android:launchMode="singleTop"]
in Android manifest.Now add the following flags while launching
A
from anywhere. It will clear the stack.