0

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?

jjj
  • 605
  • 1
  • 9
  • 26
ilyasse
  • 1
  • 1
  • 1
  • Can you be more specific by giving an example ? – PedroAGSantos May 10 '11 at 10:25
  • for example we have 3 activitis A B C, i have to navigate between theme, but i want to close my application from A by clicking on a Button in this acitiviti – ilyasse May 10 '11 at 10:28
  • I think you need some exit application type functionality in you application . but as you hv mention it is not possible because without starting or creating an Activity how can you close it. What you want can be possible by adding the Activity reference to an ArrayList and on button click you can start a loop and finish all activity. – Sujit May 10 '11 at 10:37
  • how to ad the Activity reference to an ArrayList ? – ilyasse May 10 '11 at 14:15
  • `static ArrayList allActivities = new ArrayList();` in the main activity's declarations, and after that `allActivities.add(this);` in the activity's constructor, and `allActivities.remove(this);` in the activity's onDestroy(). – Joel May 11 '11 at 00:37
  • possible duplicate of [Closing several android activities simultaneously](http://stackoverflow.com/questions/2461949/closing-several-android-activities-simultaneously) – David Grayson Aug 31 '12 at 04:17

6 Answers6

3

Please try the following method.

    this.finish();
    this.moveTaskToBack(true);
Srivathsan
  • 519
  • 4
  • 18
  • If you use this solution, on more than one running activities, itt will close the app, but it is still running in the background. And if you again start the application, itt will move to the active activity. – Zsivics Sanel Sep 16 '13 at 10:21
3

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.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
1

Try this

    Intent intent = new Intent(getApplicationContext(),FirstActivityClass.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);              
    startActivity(intent);
Dinesh
  • 566
  • 4
  • 16
0

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...!!!

Maddy Sharma
  • 4,870
  • 2
  • 34
  • 40
0

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

Flavio
  • 6,205
  • 4
  • 30
  • 28
0

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();
Joel
  • 3,427
  • 5
  • 38
  • 60
  • 1
    not a good solution IMHO: you are keeping a reference to all activities you are starting, with the risk of prolonging their life cycles far beyond their usefulness (possibly wasting *huge* amounts of memory...), unless you are *very* careful at removing them from the vector at the right time! – Rick77 Jun 11 '13 at 21:12