BACK, HOME, and Your Process

So far, our sample apps have had just one activity. So, imagine this scenario: from the app drawer, the user taps on the icon associated with your app’s activity. Then, with your activity in the foreground, the user presses BACK.

At this point, the user is telling the OS that she is done with your activity. Our activity is destroyed. Control will return to whatever preceded that activity — in this case, the launcher.

You might think that this would cause your process to be terminated. After all, that is how most desktop operating systems work. Once the user closes the last window of the application, the process hosting that application is terminated.

However, that is not how Android works. Android will keep your process around, for a little while at least. This is done for speed and power: if the user happens to want to return to your app sooner rather than later, it is more efficient to simply bring up another copy of your activity again in the existing process than it is to go set up a completely new copy of the process. This does not mean that your process will live forever; we will discuss when your process will go away later in this chapter.

Now, instead of the user pressing BACK, let’s say that the user pressed HOME instead. Visually, frequently there is little difference: the launcher re-appears.

The difference is what happens to your activity.

When the user presses BACK, your foreground activity is destroyed. It will be called with onDestroy() among the other teardown lifecycle methods. And the activity itself — the instance of your subclass of Activity — will never be used again, and hopefully is garbage collected.

When the user presses HOME, your foreground activity is not destroyed… at least, not immediately. It remains in memory. If the user launches your app again from the launcher, and if your process is still around, Android will simply bring your existing activity instance back to the foreground, rather than having to create a brand-new one (as is the case if the user pressed BACK and destroyed your activity).

What HOME literally is doing is bringing the launcher’s own activity back to the foreground. Otherwise, it does not affect your process very much.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.