Chapter 5. The Activity Lifecycle: Being an Activity

image

Activities form the foundation of every Android app.

So far you’ve seen how to create an activity, and use it to interact with the user. But if you don’t know about the activity lifecycle, some of its behavior might take you by surprise. In this chapter, you’ll learn what happens when an activity is created and destroyed, and how this can lead to unexpected consequences. You’ll find out how to control its behavior when it’s made visible, or hidden. You’ll even discover ways of saving and restoring your activity’s state, just when you need it. Read on to find out more…

How do activities really work?

So far you’ve seen how to create an interactive app by specifying how the app should behave in its activity code. But there are still lots of things you don’t yet know about how activities function.

How long does an activity live for? What happens when your activity disappears from the screen? Is it still running? Is it still in memory? And what happens if your app gets interrupted by an incoming phone call?

You want to be able to control the behavior of your activities in a whole range of different circumstances, so we’re going to take a closer look at how activities work under the hood. We’ll look at some common ways in which your apps can break, and how you can fix them using the activity lifecycle methods ...

Get Head First Android Development, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.