Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all activity in Stack when press back button

I have a list of activities A - B -C -D - E and more, for example final activity is K. I want clear all these activities in stack when i press BACK button. How can i do ? In fact, i over ride

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

but only current activity is deleted and application exit. Then, i come back application, it resume activity before K. I want it start from begining when i re-open app. I think the reason here is because the list of activities in stack still are stored, so i want to clear all stack when clicking BACK button. Any suggestions ? thank you very much !

like image 492
user3214487 Avatar asked Dec 12 '22 06:12

user3214487


2 Answers

There is method called finishAffinity() for finishing all activity.

public void onBackPressed(){
  super.onBackPressed();
  this.finishAffinity();}
like image 61
Shailesh Baldaniya Avatar answered Dec 13 '22 20:12

Shailesh Baldaniya


You need to call your activity with the FLAG_ACTIVITY_CLEAR_TOP inside your onBackPressed

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

Hope it Helps!

like image 45
Giacomoni Avatar answered Dec 13 '22 21:12

Giacomoni