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
When you launch the second activity, finish()
the first one immediately:
startActivity(new Intent(...));
finish();
I use this method to close the Activities!
public static void closeAllBelowActivities(Activity current) {
boolean flag = true;
Activity below = current.getParent();
if (below == null)
return;
System.out.println("Below Parent: " + below.getClass());
while (flag) {
Activity temp = below;
try {
below = temp.getParent();
temp.finish();
} catch (Exception e) {
flag = false;
}
}
}
It is not recommended, but you can still use this. Better go with this solution in case you need to quit the app.
According to me, the best solution is to finish every activity in your app like below.
Step 1. Maintain a static variable in mainactivity. Say,
public static boolean isQuit = false;
Step 2. On click event of an button, set this variable to true.
mainactivity.isQuit = true;
finish();
Step 3. And in every activity of your application, have the onrestart
method as below.
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
if(mainactivity.isQuit)
finish();
}
It's actually quiet easy.
The way I do this is by saving a flag in a static variable available to all. Then, when I exit, I set this flag and all my activities check this flag onResume
. If the flag is set then I issue the System.exit
on that activity.
That way all activities will check for the flag and will close gracefully if the flag is set.
Android has a mechanism in place to close an application safely per its documentation. In the last Activity that is exited (usually the main Activity that first came up when the application started) just place a couple of lines in the onDestroy() method. The call to System.runFinalizersOnExit(true) ensures that all objects will be finalized and garbage collected when the the application exits. For example:
public void onDestroy() {
super.onDestroy();
/*
* Notify the system to finalize and collect all objects of the
* application on exit so that the process running the application can
* be killed by the system without causing issues. NOTE: If this is set
* to true then the process will not be killed until all of its threads
* have closed.
*/
System.runFinalizersOnExit(true);
/*
* Force the system to close the application down completely instead of
* retaining it in the background. The process that runs the application
* will be killed. The application will be completely created as a new
* application in a new process if the user starts the application
* again.
*/
System.exit(0);
}
Finally Android will not notify an application of the HOME key event, so you cannot close the application when the HOME key is pressed. Android reserves the HOME key event to itself so that a developer cannot prevent users from leaving their application.
You should really think about not exiting the application. This is not how Android apps usually work.