How to force stop my android application programmatically?
Asked Answered
P

7

29

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.

Papilloma answered 24/2, 2011 at 5:28 Comment(4)
Don't do it. Read here why: #2034414Allina
Why do you need to do this? Best practice is to let the OS manage the application lifecycle, including process death.Centrepiece
This finishes the Actvity and not the application.Gambia
Possible duplicate of How to force stop an Android appBrandibrandice
C
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.

Centrepiece answered 24/2, 2011 at 5:39 Comment(7)
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.Allina
I want to force stop other application. This method does not works.Leontineleontyne
@AndroidDeveloper - Regarding killing another application, see this question.Centrepiece
This is not a good option. Google recommend that this should not be done. Use the following solution: https://mcmap.net/q/480083/-how-to-force-stop-my-android-application-programmaticallyOxus
@Oxus - 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.Centrepiece
Thanks for your explanation, @Ted Hopp. Now I understand your point. ThanksOxus
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.Lani
O
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

Oxus answered 17/12, 2015 at 16:54 Comment(4)
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.Scofflaw
You're right! I'm going to mention that in the answerOxus
@Vinoth Vino Glad it helpsOxus
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!Inaccessible
D
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.

Deponent answered 24/2, 2011 at 5:29 Comment(3)
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????Selfrenunciation
@Selfrenunciation - 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.Deponent
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 @Deponent thanks!Selfrenunciation
U
1

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.

Uppity answered 9/5, 2019 at 9:13 Comment(1)
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.Centrepiece
S
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);
Slip answered 22/7, 2020 at 11:8 Comment(0)
B
-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();
    }
Burack answered 1/2, 2017 at 11:49 Comment(1)
Because you would need root access?Vampire
G
-1

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

finishAffinity()

How to force stop my android application programmatically?

Greengage answered 2/9, 2021 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.