mstfdz mstfdz - 19 days ago 11
Android Question

how to finish all activities except the first activity?

I google it but even if i run this code below it didnt finish the other activities.

ButtonClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
LoginManager.getInstance().ctrl = false;
UserManager.getInstance().loginControl();
OrderManager.getInstance().orderCtrl = false;
Intent intent = new Intent(OrderComplete.this,
MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();
}
});
}

Answer

As per Our Discussion in Comments

Your Given Code is Fine !

Q1. Why its Not Finishing All the Activities ?

Ans. I think All Activities Are Finished Except Activities who have Thread or AysncTask running in Background or not Finished yet!

Q2. How Can i Finish Them ?

Ans. Make Sure your Thread Should be Finished ! or You can Try Timeout etc !


Additional

Pass String with Your Intent 

for this Add Code

intent.putExtra("finishingallact", "yes");

Inside First Activity (Which is not to be Finished)

Try by Making Object of Remaining Activities to Finish Them !

So Try code :

if(getIntent().getStringExtra("finishingallact")!=null)
{
 if(getIntent().getStringExtra("finishingallact").toLowerCase().equals("yes"))
  {
    yourRemainingAct act1=new yourRemainingAct();
    act1.finish();
    // Try Same For ALl Remaining Activities 
  }
}
Comments