Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quit android application programmatically

Tags:

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?

like image 613
Vignesh Avatar asked Jun 13 '11 12:06

Vignesh


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 quit an app on Android?

Close one app: Swipe up from the bottom, hold, then let go. Swipe up on the app. Close all apps: Swipe up from the bottom, hold, then let go.

Which method is used to terminate Android application?

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

How do I stop apps from running in my Android library?

If you want app to be stopped immediately then you have to press the Red stop button. Play button will stop the app only after gradle build is finished.


1 Answers

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 into 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.


Since API 21 you can use a very similar command

finishAndRemoveTask(); 

Finishes all activities in this task and removes it from the recent tasks list.

like image 76
sivi Avatar answered Oct 11 '22 13:10

sivi