Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();         }     }); } 
like image 819
mstfdz Avatar asked Aug 26 '13 10:08

mstfdz


People also ask

How do you finish multiple activities?

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.

How do you finish an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do I end an activity in another class?

Say finish(); Like this you can call Activity's finish method from another class. It is perfectly working.


2 Answers

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

like image 104
Kimmi Dhingra Avatar answered Sep 21 '22 00:09

Kimmi Dhingra


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.

like image 30
Avijit Avatar answered Sep 21 '22 00:09

Avijit