7

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.

Garima Tiwari
  • 1,490
  • 6
  • 28
  • 46

4 Answers4

14

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Just to be sure. This will clear all stack trace ONLY in the case of a back press event right? Not as it leads from forward from one activity to another right? – Garima Tiwari May 28 '13 at 03:47
  • That's correct. The activities in the stack will remain there until the user presses HOME or BACK or one of your activities calls `finish()`, in which case, all the `noHistory` activities will be destroyed. – David Wasser May 28 '13 at 09:08
  • But once you are back to root activity, pressing back from root activity will display root activity again wouldn't it ? @DavidWasser – Kaushik Dec 27 '16 at 16:41
  • @KaushikS No. Why would it do that? It would only do that if your root activity was in the task stack twice! – David Wasser Dec 27 '16 at 19:20
  • 1
    @DavidWasser Yeah sorry my bad. I did a stupid thing in my code. – Kaushik Dec 27 '16 at 19:59
  • what happens to all the fragments in that activity? – johnrao07 Sep 24 '20 at 20:36
  • @johnrao07 sorry, don't undertstand your comment. Please be more specific. – David Wasser Sep 24 '20 at 20:46
  • @DavidWasser There are child fragments in the activity. What happens to those? they keep running in the background? – johnrao07 Sep 24 '20 at 20:49
  • @johnrao07 when an `Activity` is destroyed, all `Fragments` belonging to that `Activity` are also destroyed. – David Wasser Sep 25 '20 at 09:28
1

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();
    }
}
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • No. I want to finish all the activities ONLY when the user presses the back button, not as I go forward. I basically want the user to be thrown back to the home screen and start the entire procedure again in case he presses the back button anytime in the middle of the transaction. The issue I'm facing right now is that I've used the code I've posted above. But after i quit the application and launch it again. It opens some random page or the page i last quit the application on. – Garima Tiwari May 27 '13 at 10:07
  • Hey, this is an interesting solution but a little off track for me. Thankyou for your efforts anyway! +1! – Garima Tiwari May 28 '13 at 03:40
0

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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • My application is basically going to be like a transaction equivalent to a credit card purchase or something, I cant have my users going back, that's a necessary requirement whether it conforms to android norms or not. Also I'm talking about clearing the stack trace as i press the back button so any help in that direction will be helpful. – Garima Tiwari May 27 '13 at 09:05
  • @GarimaTiwari did you check the last link i posted in the answer. But it is a bad design. – Raghunandan May 27 '13 at 09:08
  • I know, I admit it might be bad design but I cant have it any other way. Also yes, I did read up the link you posted but its not what im looking for. – Garima Tiwari May 27 '13 at 09:16
  • @GarimaTiwari in every activity you can have the code in the link that you saw. When user back presses he will navigate to main screen – Raghunandan May 27 '13 at 09:19
  • That is exactly the code that I am using! but somehow after I quit my application and launch it again it restarts at the same page where I last quit my application, I don't see any reason why this should be happening since that is the standard code to clear any previous stack trace. – Garima Tiwari May 27 '13 at 10:09
  • @GarimaTiwari i can't think of any other reason myself. I can't help you any further since i am not aware of the reason – Raghunandan May 27 '13 at 10:11
0
@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!

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
Cofe Code
  • 1
  • 1