Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear activity stack trace on back press event?

I have created an application that has multiple pages and navigation from one to another represents a crucial flow. I don't want the user to be able to press the back button and escape the activity without first warning him and then finally deleting all stack trace such that when the activity is launched again it starts afresh.

As of yet I have been using something similar to the function below :

    @Override
    public void onBackPressed() 
    {
        this.finish(); 
        Intent int1= new Intent(this, Home.class);
        int1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(int1);
        super.onBackPressed();
    }

But sometimes when after quitting the application when I launch it again it restarts from some random page or the one from where I quit the application (basically not the home screen from where it is expected to start)

I cannot think of a cleaner way to quit the application other than clearing all the previous activity flags as described in the code. Any help on the above is appreciated!

EDIT :

Anytime during the flow of my activity if the user presses the back button, I want the control to be thrown back to the main page (clearing all the previous activity stack traces). Such that in case someone re-lanches the application it will re start normally from the main page.

like image 811
Garima Tiwari Avatar asked May 27 '13 08:05

Garima Tiwari


People also ask

How do I start activity and clear back stack?

Declare Activity A as SingleTop by using [android:launchMode="singleTop"] in Android manifest. Now add the following flags while launching A from anywhere. It will clear the stack.

How do I delete a stack in activity?

In a case like that you can set the FLAG_ACTIVITY_CLEAR_TOP flag for the intent, meaning if the activity being launched is already running in the current task (LoginActivity), then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be ...


2 Answers

You don't need any of this custom code in onBackPressed(). All you need to do is add this to all of your <activity> definitions in the manifest (except the root activity):

android:noHistory="true"

This ensures that none of your activities (expect the root activity) is recorded in the back stack. When the user clicks the BACK key, it will just return to the root activity.

Another benefit of this is that if the user leaves your app (by clicking HOME or by pulling down the notification bar and clicking on a notification, when he returns to your app it will also just return to your root activity.

like image 138
David Wasser Avatar answered Sep 19 '22 02:09

David Wasser


Anytime during the flow of my activity if the user presses the back button, I want the control to be thrown back to the main page (clearing all the previous activity stack traces).

This can be done just by finishing all the activities as they move forward, except the MainActivity.

Such that in case someone re-lanches the application it will re start normally from the main page.

Is it like if user is in Activity_5 and uses Home Button and relaunches the app again, MainActicity must appear?

IF so, you can call finish() in onPause() of every Activity except MainActivity

EDIT:

Might not be the perfect solution, but this is what I did to achieve exactly the same(logout in my application):

OnBackPressed() in any activity updates a boolean shared preference say backPressed to true and in onResume() of all the Activities, except MainActivity check its value and finish if true.

@Override
protected void onResume() {
    super.onResume();

    SharedPreferences mSP = getSharedPreferences(
            "your_preferences", 0);
    if (mSP .getBoolean("backPressed", false)) {
        finish();
    }
}
like image 29
Archie.bpgc Avatar answered Sep 21 '22 02:09

Archie.bpgc