I've 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 but the first activity is automatically displayed instead of come back to the home screen. How can I avoid this feature ?
When you launch the second activity, finish()
the first one immediately:
startActivity(new Intent(...));
finish();
You can also specify noHistory = "true" in the tag for first activity or finish the first activity as soon as you start the second one(as David said).
AFAIK, "force close" kills the process which hosts the JVM in which your application runs and System.exit() terminates the JVM running your application instance. Both are form of abrupt terminations and not advisable for normal application flow.
Just as catching exceptions to cover logic flows that a program might undertake, is not advisable.
You can not do System.exit(), it's not safe.
You can do this one: Process.killProcess(Process.myPid());
Short answer: call moveTaskToBack(true)
on your Activity
instead of System.exit()
. This will hide your application until the user wants to use it again.
The longer answer starts with another question: why do you want to kill your application?
The Android OS handles memory management and processes and so on so my advice is just let Android worry about this for you. If the user wants to leave your application they can press the Home button and your application will effectively disappear. If the phone needs more memory later the OS will terminate your application then.
As long as you're responding to lifecycle events appropriately, neither you nor the user needs to care if your application is still running or not.
So if you want to hide your application call moveTaskToBack()
and let Android decide when to kill it.
You should really think about not exiting the application. This is not how Android apps usually work.
android.os.Process.killProcess(android.os.Process.myPid()); works fine but recommended is to let android platform worry about the memory management:-)