views:

4943

answers:

6

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 ?

+1  A: 

When you launch the second activity, finish() the first one immediately:

startActivity(new Intent(...));
finish();
David Hedlund
I dont't want to finish the first activity systematically, only if I call the System.exit(0).It's a normal feature ? How the force close button in the Application manager does work ?
Arutha
oh. i misread your question then, sorry. that's not how the standard android app lifecycle looks. the FC button uses something similar `android.os.Process.killProcess(android.os.Process.myPid());` which is not how you normally want your apps to exit. but you might use that, or you might consider looking into `startActivityForResult` and `finishActivity`, and building something where the first activity finishes only if the second one finishes with a certain response code.
David Hedlund
+2  A: 

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.

Samuh
There is no way to properly close an application and all activities still open?
Arutha
No, there's not really an "application" concept in Android; your apps are a collection of Activities. If you don't want to go back to your first activity after finishing the second, then you should use the `noHistory` attribute as mentioned.
Christopher
It is more about activities and Tasks... http://developer.android.com/guide/topics/fundamentals.html#acttask
Samuh
+1  A: 

You can not do System.exit(), it's not safe.

You can do this one: Process.killProcess(Process.myPid());

Cytown
+3  A: 

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.

Dave Webb
For more information on Dave's argument, take a look at http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238
CommonsWare
+1  A: 

You should really think about not exiting the application. This is not how Android apps usually work.

Romain Guy
Since there is no exit in an android app. Is there a way I can traverse through all the activities of an app so that I call Finish() on each i.e. finish all the activities that have been started ?
Arutha
Why do you need to do this? Are the activities in an improper state because you have logged out or some other condition? You can check for this within `onResume` and then `finish` the activity.
James
+1  A: 

android.os.Process.killProcess(android.os.Process.myPid()); works fine but recommended is to let android platform worry about the memory management:-)

Arnab