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