What happens when all activities of an application finishes?

android, android-activity, process

Solution

1) No, Android does not guarantee so. It's up to the OS to decide whether to terminate the process or not.

2) Because the `Activity` instance still lives in the Dalvik VM. In Android each process has a separate Dalvik VM.

Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications.

When you call `finish()` this doesn't mean the `Activity` instance is garbage collected. You're telling Android you want to close the `Activity` (do not show it anymore). It will still be present until Android decides to kill the process (and thus terminate the DVM) or the instance is garbage-collected.

Android starts the process when any of the application's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other applications.

3) I wouldn't do so unless you have some very strong reason. As a rule of thumb, you should let Android handle when to kill your application, unless there's something in your application state that requires an application reset when it loses focus.

Quotes source

Problem

Scenario: I've four activities in my Android Application, lets say A, B, C and D. There is one `Constants.java` class in the app which extends `Application` class in order to maintain global application state. The Constants class have all the constants variables of the app. The activity flow is like this `A-->B-->C-->D`. When back button is being pressed from Activity A, I'm calling finish() method which will finishes the activity A and closes the application. After that if I'm opening the app from all apps, there is a variable in `Constants.java` whose value persists from the last launch. The same thing is not happening when I'm doing `System.exit(10)` followed by `Process.killProcess(Process.myPid())` from activity A(on back pressed). Questions: - Will finishing all activities by calling finish() of each activity will close the Application(Its process)? - How the value of a variable persists even if its all activities are finished(closed)? - Is it fair to call `System.exit(10)` followed by `Process.killProcess(Process.myPid())` for exiting the application? Update: How can I clear the application constants on exit of the application(Back press of the HomeActivity)?

Original source

Related problems