How we handle the lifecycle phases
When we are programming an app, how do we interact with this complexity? The good news is that the Android code that was autogenerated when we created our first project does most of it for us.
As we have discussed, we just don't see the functions that handle this interaction, but we do have the opportunity to override them and add our own code to that phase if we need to.
This means that we can get on with learning Kotlin and making Android apps until we come to one of the occasional instances where we need to do something in one of the phases.
Note
If our app has more than one activity, they will each have their own lifecycle. This doesn't have to complicate things and, overall, it will make things easier for us.
The following list offers a quick explanation of the functions provided by Android to manage the lifecycle phases. To clarify our discussion of lifecycle functions, they are listed next to their corresponding phases that we have been discussing. However, as you will see, the function names make it clear on their own where they fit in.
In the list, there is also a brief explanation or suggestion for why we might use each function to interact during each phase. We will meet most of these functions as we progress through the book; we have, of course, already seen onCreate
:
onCreate
: This function is executed when the Activity is being created. Here, we get everything ready for the app, including the UI (such as callingsetContentView
), graphics, and sound.onStart
: This function is executed when the app is in the starting phase.onResume
: This function runs afteronStart
, but can also be entered (most logically) after our Activity is resuming after being previously paused. We might reload the 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
: 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 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, the Activity is 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 the Activity reaches this stage, it will need to go through the lifecycle phases from the beginning the next time the app is used.
The following diagram shows the likely flows of execution between the functions:
All the function descriptions and their related phases should appear straightforward. The only real question is: what about the running phase? As you will see when we write our code in the other functions and phases, the onCreate
, onStart
, and onResume
functions will prepare the app, which then persists, forming the running phase. Then, the onPause
, onStop
, and onDestroy
functions will occur afterward.
Now we can look at these lifecycle functions in action with a mini-app. We will do so by overriding them all and adding a Log
message and a Toast
message to each. This will visually demonstrate the phases that our app passes through.