Lifecycle phases – what we need to do
When we are programming an app, how do we possibly interact with this complexity? The good news is that the Android code that was auto-generated when we created our first project does most of it for us. As we have discussed, we just don't see the methods that handle this, but we do have the opportunity to override them and add our own code to that phase.
This means we can get on with learning Java and making Android apps until we come to one of the few instances for which we need to do something, specifically in one of the phases.
Tip
Each Activity has its own lifecycle
Actually, this discussion is relevant to just an Activity. So, if our app has more than one Activity, they will each have their own lifecycle. This doesn't have to complicate things, and in the long run it will make things easier for us.
Here is a quick explanation of the methods provided by Android, for our convenience, to manage the lifecycle phases. To clarify our discussion of lifecycle methods, they are listed next to their corresponding phases, which we have been discussing. However, as you will see, the method names make it fairly clear on their own where they fit in.
There is also a brief explanation or suggestion about when we might use a given method, and thereby interact during a specific phase. We will meet most of these methods as we progress through the book. We have of course already seen onCreate
:
onCreate
: This method is executed when the Activity is being created. Here we get everything ready for the app including UI (such as callingsetContentView
), graphics, and sound.onStart
: This method is executed when the app is in the starting phase.onResume
: This method runs afteronStart
but can also be entered, perhaps most logically, if our Activity is resumed after being previously paused. We might reload previously saved user data (such as an important note) from when the app had been interrupted, perhaps by a phone call or the user running another app.onPause
: You are probably getting the hang of these methods. This occurs when our app is pausing. Here we might save unsaved data (such as the note) that could be reloaded inonResume
. Activities always transition into a paused state when another UI element is displayed on top of the current activity (for example, a pop-up dialog), or when the activity is about to be stopped (for example, when the user navigates to a different activity).onStop
: This relates to the stopping phase. This is where we might undo everything we did inonCreate
, such as releasing system resources or writing information to a database. If we reach here, we are probably going to get destroyed sometime soon.onDestroy
: This is when our activity is finally being destroyed. There is no turning back at this phase. This is our last chance to dismantle our app in an orderly manner. If we reach here, we will definitely be going through the lifecycle phases from the beginning next time.
All the method descriptions and their related phases should appear straightforward. Perhaps the only real question is: what about the running phase? As we will see when we write our code in the other methods/phases, the onCreate
, onStart
, and onResume
methods will prepare the app, which then persists, forming the running phase. Then the onPause
, onStop
, and onDestroy
methods will occur afterwards. Now, we can actually take a look at these lifecycle methods in action. We will do so by overriding them all and adding a Log
message and a Toast
message to each.