I have four activities a,b,c,d. Activity a is the main activity which then calls b,c and d which then does some computation. I know I can prevent the activity from building up in my application by android:noHistory="true"
attribute in those activites . But the issue is if I am in activity c,when I press the back button I want to go to b, but if d finishes I want b and c to be removed and only a to displayed. Is there any way to achieve this?

- 2,041
- 6
- 27
- 32
-
Have you already coded for the back button in each activity? – jcw Nov 10 '12 at 08:22
-
Nope. I havent overridden the back button as I think its best to leave the function of the back button to the android system – user1667307 Nov 10 '12 at 08:29
2 Answers
Put this code in each of your activities, changing it slightly each time. That way you override the back button's normal function
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
//start the desired activity
}
return super.onKeyDown(keyCode, event);
}
Also, if you are only supporting android 2.0+ then you can use this
@Override
public void onBackPressed() {
// start your desired activity here
return;
}
Just put either of these in each of your activities, then start the correct activity
To close one specific activity, look at this page - How to clear specific activity from the stack history?
-
The issue is not that I want to override the back button but to close n activities which have already piled up onto my stack. – user1667307 Nov 10 '12 at 08:40
-
-
Nope. This will simply close all activites - If I am in activity c and I use finish() it will close b when I want to close activity b,c only if activity d has finished. I guess my best bet at this time is to use startActivityForResult() in my activities – user1667307 Nov 10 '12 at 10:18
-
http://stackoverflow.com/questions/5001882/how-to-clear-specific-activity-from-the-stack-history – jcw Nov 10 '12 at 10:24
-
Following the other question/answer he has linked, see mdelolmo's answer, not the accepted answer. It shows how to clear the stack. :) – IAmGroot Nov 10 '12 at 10:32
-
Yep. That kind of addresses what I want but the problem is there is no condition I can compare with to add the flag. I dont always want the activity stack to be cleared. Only if activity d has been executed. Any ideas? – user1667307 Nov 10 '12 at 10:37
-
-
Okay. I did it using startActivityForResult(). I guess that is the most efficient way of doing it. Thanks for the help – user1667307 Nov 10 '12 at 11:25
Yes it can be acheived by making sure that you use a finish() function after every startActivity function. Except in the activity which you want to be displayed in the end.
So... Here is how it looks like
Code in A:
startActivity(Intent to start activity B);
Code in B:
startActivity(Intent to start activity C);
Finish();
Code in C:
startActivity(Intent to start activity D);
Finish();
This will ensure that whenever Activity D is completed, activity C will also be completed because of Finish. As this C is also completed, Finish() in activity B will close B. As there is no Finish() in Activity A, that activity will be displayed on the screen.
If you want to come to Activity A only after D is completed, you will have to use startActivityForResult and process the result and use IF to conditionally execute Finish statement in B & C.

- 4,339
- 2
- 27
- 51