How to clean back stack on Android API 10 (Android 2.3.3)
Asked Answered
S

2

4

I would like to know how can I clean all previous activities of the stack (except the new one), but I want that in Android API 10 (Android 2.3.3).

Guided with this answer, I know it is not directly possible because the flag dedicated to do that exists since API 11.

But I would like to know if this is possible maybe with some compatibility or if someone has any solution.

Thanks in advance.

Sukey answered 2/9, 2012 at 8:22 Comment(1)
Do you want to always do this? Or just in certain situations. Because if you always want ativities to go away when you start a new activity you can just call finish() on the current activity when you call startActivity() for the next one.Magruder
M
6

On way to do this is to always start your activities using startActivityForResult(). In the case where you want to clean the activity stack have the current activity call setResult(RESULT_CANCELED) and then call finish(). In all activities (except your main or "root" activity) have the following method:

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_CANCELED) {
        // Want to clear the activity stack so I should just go away now
        setResult(RESULT_CANCELED); // Propagate result to the previous activity
        finish();
}

This will finish all activities in the stack.

Magruder answered 4/9, 2012 at 15:37 Comment(1)
Thank you @David Wasser, that's the thing!Sukey
C
0

You could add in manifest file android:noHistory="true" to each activity that you don't want to keep in the stack

Camembert answered 2/9, 2012 at 8:27 Comment(1)
This is usally a bad idea. This will prevent the user from using the "BACK" key to go back to a previous activity. Also, if the user ever navigates away from the application (to answer a phone call, for example), he won't be able to return to the app where he was. It will start all over again. Not recommended.Magruder

© 2022 - 2024 — McMap. All rights reserved.