O'Reilly logo

Programming Android by Zigurd Mednieks, G. Blake Meike, Masumi Nakamura, Laird Dornin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Component Life Cycles

Earlier we mentioned life cycles for components. For instance, onCreate starts an application.Component life cycles have two purposes: they facilitate efficient use of each application’s memory, or heap space, and they enable the state of entire processes to be preserved and restored so that the Android system can run more applications than can fit in memory.

The Activity Life Cycle

The most complex component life cycle is the activity life cycle. Here we will diagram it and take a look at how these state transitions are handled in code. In Figure 3-5, you see the states and state transitions in the activity life cycle. The key elements of handling life cycle state transitions are selecting which life cycle callbacks you need to implement, and knowing when they are called.

Activity life cycle states

Figure 3-5. Activity life cycle states

In Chapter 11, we will revisit this topic in detail. For now, let’s look at two methods of the Activity class. The runtime calls the first to warn your application to save its state. It calls the second to allow a new Activity instance to restore the state of one that has been destroyed. The method implementations in the following code snippets are taken from Chapter 11, where you can see the full program listing, including the member variables to which the code refers:

@Override protected void onSaveInstanceState(Bundle outState) { // Save instance-specific ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required