Chapter 4. The Activity Lifecycle: Being an Activity

Activities form the foundation of every Android app.
So far you’ve seen how to create activities, and made one activity start another using an intent. But what’s really going on beneath the hood? In this chapter, we’re going to dig a little deeper into the activity lifecycle. What happens when an activity is created and destroyed? Which methods get called when an activity is made visible and appears in the foreground, and which get called when the activity loses the focus and is hidden? And how do you save and restore your activity’s state?
How do activities really work?
So far you’ve seen how to create apps that interact with the user, and apps that use multiple activities to perform tasks. Now that you have these core skills under your belt, it’s time to take a deeper look at how activities actually work. Here’s a recap of what you know so far, with a few extra details thrown in.
An app is a collection of activities, layouts, and other resources.
One of these activities is the main activity for the app.

By default, each app runs within its own process.
This helps keep your apps safe and secure. You can read more about this in Appendix A (which covers the Android runtime, or ART) at the back of this book.
You can start an activity in another ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access