28

I'd like to force stop my Android application when I click closeButton. This is my code.

protected void onCreate(Bundle savedInstanceState) {

  this.setContentView(R.layout.layoutxml);

  this.closeButton = (Button)this.findViewById(R.id.close);

  this.closeButton.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View v) {

      finish();

    }

  });

}

This finishes my application. If I go to Settings -> Applications -> Manage applications -> <my application name>, I can see the 'Force Stop' button is enabled. Does this mean my application was not stopped completely?

How can I finish my Android application completely and disable the 'Force Stop' button inthe 'Settings'? From my limited experience, when an 'Exception' (ex. NullPointerException) occurs in the application, it stops abnormally, looks like it finished completely, and the 'Force Stop' button looks disabled.

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111
user573566
  • 509
  • 2
  • 6
  • 9
  • 3
    Don't do it. Read here why: http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – EboMike Feb 24 '11 at 05:35
  • 3
    Why do you need to do this? Best practice is to let the OS manage the application lifecycle, including process death. – Ted Hopp Feb 24 '11 at 05:52
  • This finishes the Actvity and not the application. – vinksharma Sep 30 '16 at 05:51
  • Possible duplicate of [How to force stop an Android app](http://stackoverflow.com/questions/29887429/how-to-force-stop-an-android-app) – Ankush Bist Feb 01 '17 at 12:27

7 Answers7

49

Another way is

android.os.Process.killProcess(android.os.Process.myPid());

I don't think it's all that bad to do this, provided you put those calls in onDestroy(). (If you kill your process in the middle of event handling, all kinds of bad things—like the touch focus going into the ether—can happen.)

Nevertheless, you need a compelling reason to deviate from best practice, which is to just call finish() and let the OS take care of killing off your process when/if it needs to.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 2
    One of the reasons why this is deprecated is because so the app starts up faster if the user switches back to it. Really, killing the process without a reason is just bad practice. – EboMike Feb 24 '11 at 06:36
  • I want to force stop other application. This method does not works. – Amit Thaper Dec 08 '15 at 10:52
  • @AndroidDeveloper - Regarding killing another application, see [this question](http://stackoverflow.com/questions/12036895/kill-another-application-on-android). – Ted Hopp Dec 08 '15 at 16:16
  • 1
    This is not a good option. Google recommend that this should not be done. Use the following solution: http://stackoverflow.com/a/34340126/2349947 – Antonio Jun 20 '16 at 20:59
  • @Antonio - Calling `finishAffinity()` (which is what your linked answer recommends) clears out the current task, but does not stop the application process, destroy the `Application` object, or release any static data. Sometimes (particularly for testing) one wants to completely restart the app. Moreover, calling `finishAffinity()` will not it accomplish what OP is trying to do. – Ted Hopp Jun 20 '16 at 21:07
  • 1
    Thanks for your explanation, @Ted Hopp. Now I understand your point. Thanks – Antonio Jun 20 '16 at 21:36
  • This is the only way working for me. My app was using a specific usb device, whatever i did, other apps using that device after i finished my app where crashing even i closed the usb device. – maggocnx Feb 14 '19 at 12:08
13

Note: This does not kill the entire app, but if what you want to do is to finish all the app activities, this is the best option.

Android ≥ 16

finishAffinity();

Android < 16

ActivityCompat.finishAffinity(Activity activity)

Hope this helps

Antonio
  • 11,413
  • 6
  • 34
  • 48
  • this is just a way to call "finish()" on more than one activity at the same time. This still does not kill the app itself. – NikkyD Jul 20 '16 at 12:28
  • You're right! I'm going to mention that in the answer – Antonio Jul 20 '16 at 15:53
  • @Vinoth Vino Glad it helps – Antonio Jan 20 '17 at 21:54
  • I find this answer solve my issue for react native 0.38 and react native router flux 3.38.0 when android.os.Process.killProcess(pid) didn't work for me; it's caused the app reopen again. Thanks! – siripan Oct 27 '17 at 05:54
8

A bad way to kill the application would be System.exit(0)

Edit: I believe I owe some explanation. Android handles the application lifecycle on its own, and you are not supposed to 'ForceClose' it, and I don't know any good way to do it. Generally its ok if your application is still alive in the background, this way if user launches it again it will pop up quickly.

m0s
  • 4,250
  • 9
  • 41
  • 64
  • what if I want to handle an exception (say something like no net detected!!) and then atop the app from going further ... because when other parts of the code begin using http results they throw nullpointerexceptions but I don't want to let the code reach there! I want to show an alert dialog letting the user know about the error and close the app because simply handling exceptions still keeps the app open..... what do I do???? – Shark Jun 17 '12 at 17:42
  • @Shark - If your program continues to execute after catching an exception, the problem is with your error handling routine. "The other parts of the code" should never get to a point of trying to use http result if there was an exception thrown. Killing an app because of an exception is a very poor method of programming... but its really fast. I think you could make a separate question for that. – m0s Jun 18 '12 at 02:20
  • 1
    I slept over it, calmed down a bit and the answer was clear! Works the way i wanted now without force killing the app. yes it was some bad and stupid coding that i had used... fixed that :) Am very new to Android and Java infact.. takes time to figure out the basics :P @m0s thanks! – Shark Jun 18 '12 at 03:57
0

I know it is a late reply , hope this helps some one.

You can try finishAndRemoveTasks(); instead of finish(); in your snippet.

This would kill your application's all activities and all process and even remove for recent apps from task manager.

Note: If you have use any kind of handler or thread in your code make sure you remove its functionalities and then use the above suggested code , if not NullPointer Exception or ResourceNotFound Exception would occur.

Suresh S
  • 41
  • 10
  • This does not close down the app's process. Static data, the Application component, and memory allocated to dynamically loaded libraries, among other things, will not be released unless the process goes away. – Ted Hopp Mar 26 '21 at 11:07
0

Short and simple

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
-1

Why not to make a Shell-Call to ActivityManager?

try {
      Runtime.getRuntime().exec("am force-stop com.me.myapp");
    } catch (IOException e) {
      e.printStackTrace();
    }
-1

The link below has the solution. Its worked for me.

finishAffinity()

How to force stop my android application programmatically?

MahmutKarali
  • 339
  • 4
  • 8