How to remove all activity in Stack when you press the back button

I have a list of actions AB -C -DE and more, for example, the final activity is K. I want to clear all these actions on the stack when I press the BACK button. How can i do In fact, I drive

onBackPress(){ 
    moveTaskToBack(true);
    finish();
}

but only the current activity and application exit is deleted. Then I return to the application, it resumes work to K. I want this to start from the beginning when I re-open the application. I think the reason here is that the list of actions on the stack is still saved, so I want to clear the entire stack by pressing the BACK button. Any suggestions? Many thanks!

+4
source share
4 answers

To complete all the actions, there is a method called finishAffinity ().

public void onBackPressed(){
  super.onBackPressed();
  this.finishAffinity();}
+5
source

You need to call your activity using FLAG_ACTIVITY_CLEAR_TOPinsideonBackPressed

@Override
public void onBackPressed()
{
    Intent it = new Intent(YouCurrentActivity.this, YourFinalActivity.class);
    it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(it);
    finish();
}

Hope this helps!

+3
source

noHistory , , .

startActivity(myIntent);
finish();

, , , Activities: Activity Fragments. , , , "".

+1

API 11 FLAG_ACTIVITY_CLEAR_TASK FLAG_ACTIVITY_NEW_TASK Intent . onBackPressed(),

> This launch mode can also be used to

FLAG_ACTIVITY_NEW_TASK: , , . , , .

, B :

Intent intent = new Intent(A.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);
finish(); // call this to finish the current activity
0

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


All Articles