I have an activity A, B, C, activity A opens activity B (A -> B) and activity B opens activity C now the back stack will be (A -> B -> C) Now Activity C opens activity B (A -> B -> C -> B) and at some condition the activity C will be destroyed in that case (A -> B -> B) is the back stack. when i click back in Activity B then the app again goes to activity B only which i don't want to do. i need to go back directly to activity A. I tried using single top but the single top works only while the activity getting created not something happens in the back stack. Any way can we achieve this ?
Asked
Active
Viewed 1,460 times
-2
-
1Post your intent code and onbackpress code that will helps to identify the issue – Amsheer Jan 31 '17 at 05:23
-
refer this link http://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity – brahmy adigopula Jan 31 '17 at 05:39
-
I am starting the activity like "this.StartActivity(newIntent);" and leaving the back press event to android, not overriding onbackpress method. – arun Jan 31 '17 at 05:40
-
This is not about closing and going back. its about if there is 2 or 3 duplicate activities exist then on clicking back in the visible activity should clear all duplicate activities. – arun Jan 31 '17 at 05:53
-
When your backstack state is A->B->C and from Activity C, if you wants to open Activity B and destroy Activity C, you should finish Activity C first and then refresh the content of existing Activity B to achieve the desired result. This will avoid your duplicate activities problem. – nnn Jan 31 '17 at 05:59
-
https://stackoverflow.com/questions/3473168/clear-the-entire-history-stack-and-start-a-new-activity-on-android https://stackoverflow.com/questions/12947916/android-remove-all-the-previous-activities-from-the-back-stack – David Rawson Jan 31 '17 at 08:41
2 Answers
0
You can try this:
override onBackPressed
method on activity B
@Override
public void onBackPressed() {
Intent i = new Intent(B.class, A.class);
i.startActivity(i);
}

Ankush Bist
- 1,862
- 1
- 15
- 32

Chariston
- 11
- 6
-1
add this code to each of your activities.
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
this code will finish your activity from stack when you move back to the last activity.

Ankush Bist
- 1,862
- 1
- 15
- 32
-
or if you are calling intent onBackPressed...that's really a bad idea. – Ankush Bist Jan 31 '17 at 05:23
-
This will finish only my current activity how can it finish the duplicate activity ? – arun Jan 31 '17 at 05:24
-
dude first think how you actually are creating the duplicate of the activity? and then avoid creating the activity again and again. – Ankush Bist Jan 31 '17 at 05:26
-
As i said in my question, i am not creating duplicate activity, instead i create activity on top of another activity but due to some condition or business rule the Activity between the duplicate items will be destroyed which leaves the duplicate activities next to each other – arun Jan 31 '17 at 05:28
-
try out this answer and override this method in all the three activities you have in your project. – Ankush Bist Jan 31 '17 at 05:31