Intro to Android

Chapter 6: Managing the Lifecycle

An application's lifecycle can be interrupted by many events, such as a user answering a phone call. It is imperative to understand what happens when a user navigates away from your phone and then comes back since this will likely happen often.

It's rare that a user will spend significant time in your application and progress through the screens exactly the way you envision. A user is typically quickly consuming or sharing content and then moving on to the next thing. It's likely that the user will exit and come back to your application many times a day. Depending on how they left your application and how you manage the lifecycle, it could lead to some surprising results when they launch your application the next time!

We often overlook this fact when we are writing applications because we stare at it for a long time and work through the application in a particular way (as it was designed). We end up only thinking about the happy path and miss many bugs that arise from improper lifecycle management. Only by interacting with your application as a normal user would will you start to see these quirks.

An application has a lifecycle. It's activities have a lifecycle. The activity may host one or more fragments that also have their own lifecycles.

We'll go the lifecycles of these components in further detail in this chapter and end with a lab to solidify these concepts.