Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force stop my android application programmatically?

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.

like image 213
user573566 Avatar asked Feb 24 '11 05:02

user573566


People also ask

How do I close a program in Android programmatically?

use 'System. exit(0);' when you really want to exit the app.

How do I force close an app on Android?

Step 1: Open the Settings apps. Step 2: Select the Apps or Apps & Notifications option. Step 3: You may need to select See all apps to view all opened applications. Step 4: Tap the application you wish to force close.

Which method is used to terminate Android application?

You can call System. exit(); to get out of all the acivities.


2 Answers

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.

like image 114
Ted Hopp Avatar answered Oct 02 '22 11:10

Ted Hopp


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

like image 31
Antonio Avatar answered Oct 02 '22 09:10

Antonio