Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when all activities of an application finishes?

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:

  1. Will finishing all activities by calling finish() of each activity will close the Application(Its process)?
  2. How the value of a variable persists even if its all activities are finished(closed)?
  3. 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)?

like image 821
Jainendra Avatar asked Mar 21 '13 09:03

Jainendra


2 Answers

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

like image 104
m0skit0 Avatar answered Nov 05 '22 08:11

m0skit0


Will finishing all activities by calling finish() of each activity will close the Application(Its process)?

No, it should not. This is because Activities are not the only component of an app. Services, BroadcastReceivers can still run without a UI. By keeping the Application process running while it can, Android ensures faster response to the user opening the app again, or faster launching of Services etc. as the Application instance doesn't need to be recreated.

Your process will only be terminated if you or another app explicitly kill it, or the system kills it to free resources.

How the value of a variable persists even if its all activities are finished(closed)?

Your variable is in the Application class, which follows a Singleton model, and hence your value will persist until the entire app process is killed.

Additionally, finish() only calls onDestroy(), which is not a deconstructor, as explained here. So even Activity variables may persist after finish has been called, as your instance is still kept around until Android feels the need to destroy it, or your process is killed.

Is it fair to call System.exit(10) followed by Process.killProcess(Process.myPid()) for exiting the application?

While this will kill the app, it is not recommended. See this question for a variety of reasons for that.

like image 26
Raghav Sood Avatar answered Nov 05 '22 07:11

Raghav Sood