Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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();
    }
}

Back Button is used to go back to the previous activity. So i would not override the back button to clear activity stack. I suggest you use a Action Bar for this purpose. Navigate to Home Screen of the application using the application icon.

http://developer.android.com/guide/topics/ui/actionbar.html

Also check this link and comments below the answer by warrenfaith

android - onBackPressed() not working for me

@Override
public void onBackPressed() 
{
    moveTaskToBack(true);
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(1);
}

you can use that code, it's work for me!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top