9

How to close an android app if more than one activity is in active state?

includeMe
  • 2,672
  • 3
  • 25
  • 39
  • What do you mean by _in active state_? – Octavian Helm Feb 28 '11 at 08:40
  • Finally i am able to exit the app. will update the methods i followed soon – includeMe Feb 28 '11 at 12:25
  • 1
    I used the following three steps: 1) Use startActivityForResult(....) instead of startActivity 2) When exit button is pressed write the following setResult(RESULT_CLOSE_ALL); finish(); 3) @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(resultCode) { case RESULT_CLOSE_ALL: setResult(RESULT_CLOSE_ALL); finish(); } super.onActivityResult(requestCode, resultCode, data); } – includeMe Feb 28 '11 at 12:33

8 Answers8

14

A blog post entitled Exiting Android Application will show how to exit an Android app:

When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "LoginActivity".

    Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);

The above code clears all the activities except for LoginActivity. LoginActivity is the first activity that is brought up when the user runs the program. Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit' message is passed.

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
    }
random
  • 9,774
  • 10
  • 66
  • 83
xydev
  • 3,409
  • 5
  • 34
  • 54
9

I got an easy solution for this problem

From the activity you press the exit button go to the first activity using the following source code. Please read the documentation for FLAG_ACTIVITY_CLEAR_TOP also.

Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

Now overide onResume() of the first activity using finish()

includeMe
  • 2,672
  • 3
  • 25
  • 39
  • hi @dsc. How can i find the first activity? I have a media player app. It shows the current track in the notifications. And when the user taps the notification it shows the Player screen. I have also a Home screen. How can i find the first activity in the back stack? – syloc Jun 07 '13 at 07:40
  • @syloc: First activity means the activity which is shown while the app is launched. – includeMe Jun 11 '13 at 12:10
  • Also you can find the first activity using ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List taskInfo = activityManager .getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).baseActivity; – includeMe Jun 11 '13 at 12:16
  • Suppose that i launched the app with the home screen. Than play a song and the song is now in the notifications. Then i press the back button until i return to the android home screen. Then i click the notification and enter the app again. But this the first activy became the Player screen. – syloc Jun 11 '13 at 12:17
  • @syloc: Edited my previous comment, componentInfo will always returns the first activity in the stack. – includeMe Jun 11 '13 at 12:22
2

The answer is simple: You really do not need to 'close' an Android application. If no activity is shown any more, the system will kill the process after some time. The users can close activities by pressing the 'back' button. Reto Meier explains it pretty well here: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html

mreichelt
  • 12,359
  • 6
  • 56
  • 70
2

You might also want to read this thread; it is very helpful to say the least: Quitting an Android application - Is it frowned upon?

Community
  • 1
  • 1
Samuh
  • 36,316
  • 26
  • 109
  • 116
2

Well, you shouldn't close your applications, as the system manages that. Refer to the posts/topics in the other answers for more information.

However, if you really, really want to, you can still call System.exit (0); like in any other Java application.

EDIT

ActivityManager actmgr = (ActivityManager) this.getSystemService (Context.ACTIVITY_SERVICE);
actmgr.restartPackage ("com.android.your.package.name");

I remembered something. I was trying to use this code to restart my application, but it only managed to kill my app. You can try it and see if it works for you.

Shade
  • 9,936
  • 5
  • 60
  • 85
  • This is true. But one should note that this call might be dangerous, especially if multiple threads are active. These will be stopped. And it might happen (and Murphy says it will happen) that the threads stop in a state which is not intended by the developer, which might lead to data corruption... – mreichelt Feb 28 '11 at 09:47
  • @mreichelt, that is why I wrote really, _really_ :) @dsc, are you sure? I believe it should work... – Shade Feb 28 '11 at 11:53
  • @Shade it doesn't work for me...if you are that much sure then I will check it again. May be of some other mistake of my app :( – includeMe Feb 28 '11 at 12:10
  • @Shade saw your answer..But that isn't the right method. When I used startActivityForResult instead of startActivity I was able to exit the app – includeMe Mar 01 '11 at 04:38
  • @Shade I was able to restart my app using that method – includeMe Mar 01 '11 at 06:36
  • @dsc, but which Activity are you starting to stop your application? – Shade Mar 01 '11 at 08:31
  • @Shade, 'FirstActivity' means the root activity of my app. Actually I'm not starting a new activity. By using 'intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)' it checks whether such an activity has already started then it will go back to that activity closing all other activities. And when I close the 'FirstActivity' the app will be closed. – includeMe Mar 02 '11 at 05:29
  • @dsc, no, I didn't see your answer at first. Nice solution :) – Shade Mar 03 '11 at 00:17
  • @Shade thanks man. Would you mind voting up that answer so it may help those who need that solution – includeMe Mar 04 '11 at 06:56
  • @dsc, I did vote your answer up, but you should probably set it as the chosen answer, as most people look only at that. – Shade Mar 04 '11 at 09:14
1

I asked a similar question a couple of weeks back. Do go through the answers and comments for more perspective and possible solutions.

IMO quitting an application depends on what your application does and the user expectations. While I understand the rationale on not having a quit button I also do believe that it's a choice that the application designer has to make based on the situation.

Community
  • 1
  • 1
Ravishankar V
  • 323
  • 1
  • 8
0

If you want to exit from one Android activity, this will bring you back to the previous activity or another activity from a specific place in current activity.

finish();
System.exit(0);
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
0

Once your last Activity looses focus, Android will unload your process according to the current system needs / free resources. You shouldn't really care about that - just use the lifecycle onStart, OnStop etc... to manage your state.

Guy
  • 12,250
  • 6
  • 53
  • 70