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(); } }); }
When the user is on Activity D and click a button called exit, the application should go back to Activity B and finish the Activities C and D.
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
Say finish(); Like this you can call Activity's finish method from another class. It is perfectly working.
To clear top activities from stack use below code
Intent intents = new Intent(A.this, B.class); intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intents); finish();
It will delete all activities from stack either asynctask run or not in the application.
It works fine and also a good approach
when your going from one activity to other activity call finish();
do like this
public void onClick(View v) { Intent i = new Intent(A.this,B.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i); finish(); // to end the current activity }
call finish() after startactivity(...), so that A activity ll removed from the stack. when you press back button A activity ll be not there in stack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With