Back and Home button behaviour

Hi! I’ve got some doubts while reading the third chapter:

The Recents button displays a list of recently used apps but it doesn’t mean they are all running in the background, am I right? It depends on how you exit a certain app: if you press the Back button, the app is cleared from memory. Instead if the Home button is pressed, the app moves to the Stopped state, still running in the background. Clearing all the apps with the Clear All button may or may not improve the overall performance because there might be no apps running in the background.
Does it make sense? Am I missing something?

Everything you’ve said is correct except for a distinction between an App and an Activity. There is a concept of an Application in Android. This application includes the process that your activities run in, the memory that they use, and any other classes that are loaded into memory. So, an application can have activities that live inside of it.

Everything you said in your message is accurate on an Activity basis. For example, hitting the back button will clear an activity from memory. It is possible for the application to still exist in memory after you’ve removed all of the activities from memory. Android will try to do this so that if the user returns to that app, new activities will be restarted quickly (and Android won’t have to recreate an application from scratch). Depending on how long it’s been since you’ve used an application, how many activities are in memory, and how much memory Android has available, Android may decide to clear out the application too.

I hope that helps.

1 Like

I’m still a bit puzzled after reading this article. Hiroshi Lockheimer states that:

in general (it is) better to let the system do its job; it (Android) was designed to manage running apps so you don’t have to

and then he suggests that things might even get worse.

Does pressing the Clear All button remove even this last “hidden” instance in memory? If this is the case, and as I understand it, pressing the Clear All button will influence negatively the performance of your phone and to counter that we should let multitasking do its job, avoiding this kind of behaviour.
My question now would be: what is its purpose then? To clear out apps that are not responsive because they crashed? To clear out a very long list of recent apps? If those are the cases I think, in the end, it is not quite well-defined. Before beginning my journey as an Android developer I thought clearing all the apps was meant to speed up your phone.

Yea, I agree with that advice. Clear all can bring small performance/battery dings. When you hit the clear all button, all of the Activities are removed from memory. It’s possible that the app’s process can stick around (especially if an app has a service running), but likely the apps will be completely removed from memory. You shouldn’t do this because that’s the operating system’s job. It’s constantly evaluating what you’re doing, what you’ve done recently, what you haven’t done, etc to free up memory and make the things that you do frequently as fast as possible. So, the OS may keep some of your app’s processes cached and ready to go because you use them all the time.

When you forcibly remove those apps that you use often from memory, it means that the next time you open that app, the whole process must be rebuilt from scratch which is a small amount of extra work (leading to slightly worse performance and battery life).

Removing an app from memory could be useful if you want to clear out all of the state on that app. Maybe you’ve navigated down a few screens in the app. Swiping that app away from the recents screen will remove that state and you’ll start over the next time you open it (also helps with apps that are not functioning correctly). Clear all could be useful if you’re running really low on memory and the OS isn’t doing a good job of keeping up. Otherwise, I don’t use the clear all option at all.

1 Like

Thank you very much Chris. Now things are getting clearer.