ilyasse
ilyasse

Reputation: 1

Close all activities from the first activity in an Android app

I have some activities in an android application and I want to close them all from the first activity.

Is there a way to do that?

Upvotes: 0

Views: 3330

Answers (6)

Sergey Glotov
Sergey Glotov

Reputation: 20336

You may call activity A with Intent with FLAG_ACTIVITY_CLEAR_TOP flag set. This brings activity A on top of activities' stack and finishes activities opened from A.

If activity A isn't root activity in application's task you may try FLAG_ACTIVITY_CLEAR_TASK flag.

Upvotes: 3

Maddy Sharma
Maddy Sharma

Reputation: 4956

If u have MainActivity>B>C>D>E activities and MainActivity is the launcher and U calls all in sequence without calling finish on anyone like :

Intent jump = new Intent(MainActivity.this, B.class);
//finish();
startActivity(jump);

steps:

1) Define static Vector act = new Vector(); in main activity(MainActivity activity) and add act.add(this);

2) Define default constructor in all activities and add references like:

public B()
{
MainActivity.allActivities.add(B.this);
}

3) Call the above code on every exit button clicking:

for (Activity a : MainActivity.allActivities) a.finish(); 
MainActivity.allActivities.clear();

I hope this will help...!!!

Upvotes: 0

Dinesh
Dinesh

Reputation: 566

Try this

    Intent intent = new Intent(getApplicationContext(),FirstActivityClass.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);              
    startActivity(intent);

Upvotes: 1

Srivathsan
Srivathsan

Reputation: 519

Please try the following method.

    this.finish();
    this.moveTaskToBack(true);

Upvotes: 3

Joel
Joel

Reputation: 3454

You can define a static vector of activities allActivities in your main activity. In the constructor of each activity, you add a reference to it into the static vector. In the destructor, you remove it.

Whenever you need to close all of them:

for (Activity a : MainActivity.allActivities) a.finish(); 
MainActivity.allActivities.clear();

Upvotes: 0

Flavio
Flavio

Reputation: 6235

AFAIK, There are no methods to close all the activities in the application, but you can simply move the task containing that activities to background (see moveTaskToBack).

Or you can try to send broadcast and in onReceive - finish the activity that received the broadcast

Upvotes: 0

Related Questions