Exit an android app
Asked Answered
M

8

9

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

Maillol answered 28/2, 2011 at 8:33 Comment(3)
What do you mean by in active state?Resource
Finally i am able to exit the app. will update the methods i followed soonMaillol
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); }Maillol
M
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()

Maillol answered 1/3, 2011 at 9:51 Comment(5)
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?Lezlielg
@syloc: First activity means the activity which is shown while the app is launched.Maillol
Also you can find the first activity using ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfo = activityManager .getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).baseActivity;Maillol
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.Lezlielg
@syloc: Edited my previous comment, componentInfo will always returns the first activity in the stack.Maillol
P
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();
    }
Proprietor answered 28/2, 2011 at 10:4 Comment(1)
check my answer if you want a simple solutionMaillol
M
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()

Maillol answered 1/3, 2011 at 9:51 Comment(5)
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?Lezlielg
@syloc: First activity means the activity which is shown while the app is launched.Maillol
Also you can find the first activity using ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfo = activityManager .getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).baseActivity;Maillol
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.Lezlielg
@syloc: Edited my previous comment, componentInfo will always returns the first activity in the stack.Maillol
M
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

Marbut answered 28/2, 2011 at 8:48 Comment(0)
P
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?

Phrasing answered 28/2, 2011 at 9:22 Comment(0)
S
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.

Sulfapyrazine answered 28/2, 2011 at 9:31 Comment(10)
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...Marbut
@mreichelt, that is why I wrote really, really :) @dsc, are you sure? I believe it should work...Sulfapyrazine
@Sulfapyrazine 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 :(Maillol
@Sulfapyrazine saw your answer..But that isn't the right method. When I used startActivityForResult instead of startActivity I was able to exit the appMaillol
@Sulfapyrazine I was able to restart my app using that methodMaillol
@dsc, but which Activity are you starting to stop your application?Sulfapyrazine
@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.Maillol
@dsc, no, I didn't see your answer at first. Nice solution :)Sulfapyrazine
@Sulfapyrazine thanks man. Would you mind voting up that answer so it may help those who need that solutionMaillol
@dsc, I did vote your answer up, but you should probably set it as the chosen answer, as most people look only at that.Sulfapyrazine
F
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.

Fascicule answered 28/2, 2011 at 11:39 Comment(0)
P
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.

Palsgrave answered 28/2, 2011 at 9:23 Comment(0)
C
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);
Constringe answered 17/7, 2012 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.