How to clear the event stack trace at a back press event?

I created an application with several pages and the transition from one to another is an important stream. I don’t want the user to be able to click the "Back" button and avoid the activity without prior warning, and then finally delete the entire stack trace so that when the activity is restarted, it starts again.

At the moment, I used 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 exiting the application when it starts, it restarts from some random page or from the one where I exit the application (basically not on the main screen, where it should start from)

I cannot think of an easier way to exit the application, except to clear all previous activity flags, as described in the code. Any help on the above is appreciated!

EDIT:

At any time during the flow of my activity, if the user clicks the back button, I want the control to be returned to the main page (clearing all previous stacks of the activity stack). Thus, if someone remakes the application, he will start normally from the main page.

+4
source share
3 answers

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

 android:noHistory="true" 

This ensures that none of your actions (waiting for root activity) is written to the back stack. When the user presses the BACK button, he simply returns to the root activity.

Another advantage of this is that if the user leaves your application (by pressing the "HOME" button or by pulling the notification panel and clicking on the notification when he returns to your application, he will also return to your root action.

+10
source

At any time during the flow of my activity, if the user clicks on the back, I want the control to be dropped to the main page (clearing all previous traces of the activity stack).

This can be done by simply completing all the actions as they move forward, except for MainActivity.

Thus, if someone remakes the application, he will usually start again from the main page.

It seems that if the user is in Activity_5 and uses the main button and starts the application again, should MainActicity appear?

IF so, you can call finish() in onPause() for every operation except MainActivity

EDIT:

It may not be the ideal solution, but this is what I did to achieve the same (exit my application):

OnBackPressed() in any activity updates the boolean general privilege : backPressed to true and onResume() all actions except MainActivity to check its value and end if true .

 @Override protected void onResume() { super.onResume(); SharedPreferences mSP = getSharedPreferences( "your_preferences", 0); if (mSP .getBoolean("backPressed", false)) { finish(); } } 
+1
source

The back button is used to return to the previous action. Therefore, I would not override the back button to clear the activity stack. I suggest you use the action bar for this purpose. Go to the main screen of the application using the application icon.

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

Also check out this link and comments below warrenfaith answer

android - onBackPressed () not working for me

0
source

Source: https://habr.com/ru/post/1482921/


All Articles