16

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)?

Community
  • 1
  • 1
Jainendra
  • 24,713
  • 30
  • 122
  • 169

4 Answers4

14

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

m0skit0
  • 25,268
  • 11
  • 79
  • 127
6

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.

Community
  • 1
  • 1
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
2

for your Update:

How can I clear the application constants on exit of the application(Back press of the HomeActivity)?

Override onBackPressed inside your HomeActivity and before call super.onBackPressed() clear all the variable. If you have a setter for a String located inside your Application subclass:

@Override
public void onBackPressed() {
    getApplication().setMyString(null);
    super.onBackPressed();
}

if you have plenty states to clear you can write a method wich reset all the fields and call it:

@Override
public void onBackPressed() {
    getApplication().clearAllMyFields();
    super.onBackPressed();
}

Of course those field have not to be marked as final or static final

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Android will keep the process until it needs to reclaim the process memory. That way, if the user immediately returns to your application, your application will appear quicker.

The value will persist until the application is killed.

Should not using System.exit(10) followed by Process.killProcess(Process.myPid()) for exiting the application?

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

Raghunandan
  • 132,755
  • 26
  • 225
  • 256