Sie sind auf Seite 1von 12

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

113

How to quit android application programmatically


android

I Found some codes for quit an Android application programatically. By calling any one of the following code in onDestroy() will it quit application
entirely?
1. System.runFinalizersOnExit(true)
(OR)
2. android.os.Process.killProcess(android.os.Process.myPid());
I dont want to run my application in background after clicking quit button. Pls suggest me can i use any one of these code to quit my app? If so which
code can i use? Is it good way to quit the app in Android?

share

Vignesh
1,251 5 13 11

Asked
Jun 13 '11 at 12:08

Michael
27.8k 10 65 86

Edited
Jun 13 '11 at 12:12

There are so many posts already here. stackoverflow.com/search?q=how+to+exit+an+android+app Mudassir Jun 13 '11 at 12:17

improve this question

possible duplicate of Quitting an application - is that frowned upon? CommonsWare Jun 13 '11 at 14:02
use 'System.exit(0);' when you really want to exit the app. I have done that on an unrecoverable error after showing the user a message. If you need to, you can even automatically
relaunch the app using AlarmManager. See: blog.janjonas.net/2010-12-20/ Someone Somewhere Aug 1 '13 at 17:52
add a comment

18 Answers

88

Order By

Votes

getActivity().finish();
System.exit(0);

this is the best way to exit your app.!!!


The best solution for me.

share

improve this answer


Devon
1,188 9 18

Answered
Sep 3 '14 at 11:03

Edited
Oct 9 '14 at 13:14

The best solution for me. Thanks Disonash. EliaszKubala Sep 5 '14 at 17:32

yeah, works great! Have to call this from MainActivity to get it to work! mz87 Oct 5 '14 at 21:59

Perfect solution. No need to terminate the process, as Android knows best for memory management. However, this provides a solution for finishing the activity, as well as exiting the
app for user convenience. IAmTheSquidward Jan 8 '15 at 5:25

I don't think this is a good solution. You oughtn't finish your program using System.exit(0);. The right solution was posted by @sivi (his answer is above) Antonio May 29 '15
at 22:53

this is not the solution, it doesn't work if you have a stack of activities user3290180 Aug 12 '15 at 17:28
show 3 more comments

83

Since api 16 you can use the finishAffinity method, which seems to be pretty close to closing all related activities by its name and javadoc
description:
this.finishAffinity();
Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an
application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up

navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second
application, all of those should be removed from the original task as part of the task switch.
Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.

share

improve this answer


sivi
2,327 1 17 22

Answered
Jan 4 '15 at 12:51

Edited
Mar 13 at 12:32

Definitely this is the right answer! It's not a good solution to finish an app using System.exit(0);. Thank you for your answer! Finally I've found a decent solution. Antonio May
29 '15 at 22:48

yes, you can use this method from every activity. System.exit(0) will only close an activity, not the stack of components user3290180 Aug 12 '15 at 17:27

Do you know if there is another solution on API 14? Thanks Script Kitty Nov 29 '15 at 23:15

You can finish() each activity individualy, either when you close the app or inside the activity lifcycle. sivi Nov 30 '15 at 6:09

This answer works for me ;) Milad Jan 31 at 12:07


show 2 more comments

29

Please think really hard about if you do need to kill the application: why not let the OS figure out where and when to free the resources?
Otherwise, if you're absolutely really sure, use
finish();

As a reaction to @dave appleton's comment: First thing read the big question/answer combo @gabriel posted: Quitting an application - is that
frowned upon?
Now assuming we have that, the question here still has an answer, being that the code you need if you are doing anything with quitting is
finish(). Obviously you can have more than one activity etc etc, but that's not the point. Lets run by some of the use-cases

1. You want to let the user quit everything because of memory usage and "not running in the background? Doubtfull. Let the user stop certain
activities in the background, but let the OS kill any unneeded recourses.
2. You want a user to not go to the previous activity of your app? Well, either configure it so it doesn't, or you need an extra option. If most of
the time the back=previous-activity works, wouldn't the user just press home if he/she wants to do something else?
3. If you need some sort of reset, you can find out if/how/etc your application was quit, and if your activity gets focus again you can take
action on that, showing a fresh screen instead of restarting where you were.
So in the end, ofcourse, finish() doesn't kill everthing, but it is still the tool you need I think. If there is a usecase for "kill all activities", I haven't
found it yet.

share

improve this answer


Nanne
44.7k 13 72 107

Answered
Jun 13 '11 at 12:13

Edited
Sep 16 '12 at 7:07

14

Calling finish() will not kill the application. finish() is used all the time: Call this when your activity is done and should be closed. It's the same effect as hitting
the "back" button. Tanner Perrien Jun 13 '11 at 12:44
Finish will kill the activity. how is this different then killing the appliation? Nanne Jun 13 '11 at 13:26

There is nothing wrong with "finishing" or "killing" the Activity (not the Application). For example, you might start a new activity to show the user some information. After a
timeout or pressing 'OK' you might call finish() to return to the calling activity. Tanner Perrien Jun 13 '11 at 13:55

25

Most Android applications have more than one activity. CommonsWare Jun 13 '11 at 14:03
@Nanne - A lock screen is an example of when you'd want to stop all activities. Suppose you log into a banking app. After a certain period of time with no interaction from the user,
the lock screen launches. You'll want to stop all activities if someone hits the back button from the lock screen! Jabari May 30 '13 at 5:13
show 5 more comments

13

I think that application should be kill in some case. For example, there is an app can be used only after login. The login activity has two
buttons, 'login' and 'cancel'. When you click 'cancel' button, it definitely means 'Terminate the app'. Nobody wants the app alive in the
background. So I agree that some cases need to shut down the app.

share

improve this answer


sunghun
537 2 14 32

Answered
Apr 13 '12 at 2:03

I total agree with this use-case. Also consider my secure app that must to be available in a certain location and should do it's best to close if the user is not at that location. What do I
do? Sydwell Jun 4 '14 at 10:43

Please add a code sample Alfred Mar 17 '15 at 13:29


add a comment

13

There is no application quitting in android, SampleActivity.this.finish(); will finish the current activity.
When you switch from one activity to another keep finish the previous one
Intent homeintent = new Intent(SampleActivity.this,SecondActivity.class);
startActivity(homeintent);
SampleActivity.this.finish();

share

improve this answer


san
220 2 8

Answered
Jun 12 '12 at 5:07

Tom Dignan
5,286 2 24 41

Edited
Oct 20 '12 at 22:46

Very good idea! Seyyed Puya Soofbaf May 29 '14 at 16:06


add a comment

public void quit() {


int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
System.exit(0);
}

share

improve this answer


meduvigo
507 5 6

Answered
Feb 22 '15 at 0:19

Edited
Mar 18 '15 at 10:05

System.exit() does not kill the Android Application if there is more than one activity Alfred Mar 17 '15 at 13:19
add a comment

I'm not sure if this is frowned upon or not, but this is how I do it...
Step 1 - I usually have a class that contains methods and variables that I want to access globally. In this example I'll call it the "App" class.
Create a static Activity variable inside the class for each activity that your app has. Then create a static method called "close" that will run the
finish() method on each of those Activity variables if they are NOT null. If you have a main/parent activity, close it last:
public class App
{
////////////////////////////////////////////////////////////////
// INSTANTIATED ACTIVITY VARIABLES
////////////////////////////////////////////////////////////////
public static Activity activity1;
public static Activity activity2;
public static Activity activity3;
////////////////////////////////////////////////////////////////
// CLOSE APP METHOD
////////////////////////////////////////////////////////////////

public static void close()


{
if (App.activity3 != null) {App.activity3.finish();}
if (App.activity2 != null) {App.activity2.finish();}
if (App.activity1 != null) {App.activity1.finish();}
}

Step 2 - in each of your activities, override the onStart() and onDestroy() methods. In onStart(), set the static variable in your App class equal to
"this". In onDestroy(), set it equal to null. For example, in the "Activity1" class:

@Override
public void onStart()
{
// RUN SUPER | REGISTER ACTIVITY AS INSTANTIATED IN APP CLASS

super.onStart();
App.activity1 = this;

@Override
public void onDestroy()
{
// RUN SUPER | REGISTER ACTIVITY AS NULL IN APP CLASS

super.onDestroy();
App.activity1 = null;

Step 3 - When you want to close your app, simply call App.close() from anywhere. All instantiated activities will close! Since you are only closing
activities and not killing the app itself (as in your examples), Android is free to take over from there and do any necessary cleanup.
Again, I don't know if this would be frowned upon for any reason. If so, I'd love to read comments on why it is and how it can be improved!

share

improve this answer


Jabari
2,013 2 13 24

Interesting, I too would be interested in what other experienced Android developers thought of this. Rudi Kershaw Feb 13 '14 at 14:20

Awesome answer, thanks a bunch! Darrell Aug 20 '14 at 16:45

Answered
May 30 '13 at 5:07

By keeping a reference to your activity you are actually leaking memory! See this for an in-depth discussion. Managarm Oct 25 '14 at 22:47
@Managarm The premise behind that post is that the static variable will remain even after the process (in this case, the Activity) has been destroyed. If proper house cleaning is
done though, it's a non-issue...hence the reason I nullify the reference in the activity's onDestroy() method. Jabari Oct 26 '14 at 7:08
@Jabari Ah, I missed the onDestroy part. In that case it should not be an issue, though it's not best practice. Managarm Oct 26 '14 at 11:53

show 1 more comment

show 1 more comment

Quitting an application - is that frowned upon?. Go through this link. It answers your question. The system does the job of killing an application.
Suppose you have two activities A an B. You navigate from A to B. When you click back button your activity B is popped form the backstack
and destroyed. Previous activity in back stack activity A takes focus.
You should leave it to the system to decide when to kill the application.
public void finish()
Call this when your activity is done and should be closed.
Suppose you have many activities. you can use Action bar. On click of home icon naviagate to MainActivity of your application. In MainActivity
click back button to quit from the application.

share

improve this answer


Raghunandan
91k 11 119 172

Answered
Oct 14 '12 at 15:30

Edited
Jun 6 '13 at 9:07

share

It's not a good decision, cause it's against the Android's application processing principles. Android doesn't kill any process unless it's
absolutely inevitable. This helps apps start faster, cause they're always kept in memory. So you need a very special reason to kill your
application's process.

improve this answer


Egor
20.9k 6 59 92

Answered
Jun 13 '11 at 12:15

sign out of an application requires to really kill the app. so there should be a way to ensure app killed after signing out anonim May 11 '12 at 0:56
add a comment

This may be very late and also as per the guidelines you're not supposed to handle the life cycle process by yourself(as the os does that for
you). A suggestion would be that you register a broadcast receiver in all your activities with "finish()" in their onReceive() & whenever you wish to

quit you can simple pass an intent indicating that all activities must shut down..... Although make sure that you do "unregister" the receiver in
your onDestroy() methods.

share

improve this answer


Aalok Sharma
704 2 9 21

Answered
Feb 19 '13 at 5:54

I think what you are looking for is this


activity.moveTaskToBack(Boolean nonRoot);

share

improve this answer


Ciprian
1,230 2 15 24

Answered
Jun 28 '13 at 8:45

From your top-level activity:


super.onBackPressed();

share

improve this answer


QED
7,002 3 27 60

This will kill anything ;)


int p = android.os.Process.myPid();
android.os.Process.killProcess(p);

share

improve this answer

Answered
May 30 '14 at 22:45

D.Snap
473 4 11

Answered
May 6 '15 at 15:16

The correct and exact solution to quit the app on button click is using the below code:
//On Button Back pressed event
public void onBackPressed()
{
moveTaskToBack(true);
finish();
}

share

improve this answer


Pir Fahim Shah
4,515 1 35 42

Answered
Sep 14 '15 at 17:39

You can use finishAndRemoveTask () from API 21


public void finishAndRemoveTask ()
Call this when your activity is done and should be closed and the task should be completely removed as a part of finishing the Activity.

share

improve this answer


Vins
1,932 2 16 39

Answered
Jan 22 at 11:00

It depends on how fast you want to close your app.


A safe way to close your app is finishAffinity();
It closes you app after all processes finished processing. This may need some time. If you close your app this way, and restart it after a short
time, it is possible that your new application runs in the same process. With all the not finished processes and singleton objects of the old
application.
If you want to be sure, that your app is closed completly use System.exit(0);

This will close your app immediatly. But it is possible, that you damage files that your app has open or an edit on shared preferences does not
finish. So use this carefully.
If you use watchdog in combination with a long running task, you can see the influences of the different methods.
new ANRWatchDog(2000).setANRListener(new ANRWatchDog.ANRListener() {
public void onAppNotResponding(ANRError error) {
MainActivity.this.finishAffinity();
System.exit(0);
}
}).start();
for(int i = 0; i < 10; ++i){
--i;
}

This kills your app after 2 seconds without displaying an ANR dialog or something like that. If you remove System.exit(0), run this code and
restart the app after it is closed, you will experience some strange behaviour, because the endless loop is still running.

share

improve this answer


user3424426
21 6

-1

share

Answered
May 10 at 15:07

Just use finish() on back key press onKeypressed()

improve this answer


khushal rasali
17 2

Answered
Sep 14 '15 at 17:43

finish() will close an Activity, but will certainly not close the entire app with certainty. 2Dee Sep 24 '15 at 13:05
add a comment

-1

Write this code in your on backpressed override method

@Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}

share

improve this answer


Raj Kumar
112 1 11

Answered
Mar 21 at 11:07

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Das könnte Ihnen auch gefallen