I have two different activities. The first launches the second one. In the second activity, I call System.exit(0)
in order to force the application to close, bu
This is what I did to close the application: In my application I have a base activity class, I added a static flag called "applicationShutDown". When I need to close the application I set it to true.
In the base activity onCreate and onResume after calling the super calls I test this flag. If the "applicationShutDown" is true I call finish on the current Activity.
This worked for me:
protected void onResume() {
super.onResume();
if(BaseActivity.shutDownApp)
{
finish();
return;
}}
You can not do System.exit(), it's not safe.
You can do this one: Process.killProcess(Process.myPid());
android.os.Process.killProcess(android.os.Process.myPid());
works fine, but it is recommended to let the Android platform worry about the memory management:-)
Start the second activity with startActivityForResult and in the second activity return a value, that once in the onActivityResult method of the first activity closes the main application. I think this is the correct way Android does it.
Use the finish
method. It is the simpler and easier method.
this.finish();
I use this:
1) The parent activity call the secondary activity with the method "startActivityForResult"
2) In the secondary activity when is closing:
int exitCode = 1; // Select the number you want
setResult(exitCode);
finish();
3) And in the parent activity override the method "onActivityResult":
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int exitCode = 1;
if(resultCode == exitCode) {
super.setResult(exitCode); // use this if you have more than 2 activities
finish();
}
}
This works fine for me.