Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We've learned a lot about the activity lifecycle. In this video we'll go through a sample app and see the activity lifecycle in action!
GitHub Link to Project
Related Links
-
0:00
[MUSIC]
-
0:04
All right, now that we've got a pretty good idea of what the activity life cycle
-
0:09
is and when each of the life cycle methods will be called,
-
0:12
let's jump into a sample app and run a few tests to see these in action.
-
0:17
Here's the app we'll be using to test out the activity life cycle.
-
0:21
Before we get started let's take a minute to see how the app works.
-
0:25
First, there's a custom activity called LoggingActivity.
-
0:29
Every activity that extends LoggingActivity
-
0:32
will log a message each time a lifecycle method is called.
-
0:36
Next we have MainActivity.
-
0:39
MainActivity is the entry point of the application.
-
0:42
And has two buttons that launch the other two activities.
-
0:46
One button launches opaque activity and
-
0:49
the other button launches transparent activity.
-
0:52
MainActivity, OpaqueActivity, and
-
0:55
TransparentActivity all extend the LoggingActivity.
-
0:59
So we should see a log message for each and
-
1:02
every life cycle method that gets called for these activities.
-
1:06
Let's run the app and see what gets called.
-
1:13
Before we start parsing the log cat, let's filter it by our tag, life cycle example.
-
1:23
Just as we would expect from the diagram, the methods called so
-
1:29
far are onCreate, onStart and onResume from MainActivity.
-
1:35
And MainActivity is currently in a running state at the top of the activity stack.
-
1:41
What do we expect to happen if we launch OpaqueActivity?
-
1:46
If OpaqueActivity is launched, MainActivity won't be visible anymore.
-
1:51
So we would expect to see onPause and onStop from MainActivity.
-
1:56
And for OpaqueActivity to be the running activity,
-
2:00
we would expect to see onCreate, onStart, and onResume from OpaqueActivity.
-
2:07
But what will be the order of these methods?
-
2:10
Take a minute to think about it.
-
2:13
Feel free to pause me, I'll be right here.
-
2:17
All right, got your guess?
-
2:19
Let's find out.
-
2:25
If you guessed MainActivity is onPause, followed by onCreate,
-
2:30
onStart, and onResume from OpaqueActivity, and
-
2:33
finishing up with MainActivity is onStop, then you're right.
-
2:39
Let's take a minute to see why this is the case.
-
2:42
OnPause is called when you can no longer interact with an activity.
-
2:46
This being called first, means main activity is removed from the top of
-
2:51
the activity stack, which is necessary if we're going to have a new activity on top.
-
2:56
Next comes onCreate, onStart.
-
2:59
And onResume, from OpaqueActivity.
-
3:03
OpaqueActivity is now on top of the activity stack, and ready for
-
3:07
user interaction.
-
3:09
Lastly, MainActivities on stop is called.
-
3:13
OnStop is called when the activity is no longer visible.
-
3:16
Since OpaqueActivity isn't visible until after onStart and
-
3:21
since onResume will immediately follow onStart.
-
3:25
MainActivity's onStop method won't be called until after
-
3:29
OpaqueActivity's onResume method.
-
3:32
Now that we know which life cycle methods were called to get us here,
-
3:35
let's hit the back button and
-
3:37
see which life cycle methods get called when we go back to Main Activity.
-
3:43
Looks like it's the same calls as last time,
-
3:46
except we've switched MainActivity and OpaqueActivity.
-
3:51
The only difference is the call to onDestroy and OpaqueActivity.
-
3:57
Leaving an activity by hitting the back button is our way of telling
-
4:01
Android that we're finished with an activity.
-
4:04
That's why Android destroys OpaqueActivity instead of leaving it in a stopped state.
-
4:10
Now that we're back in MainActivity,
-
4:12
let's see what happens when we launch the TransparentActivity.
-
4:16
Go ahead and pause me if you'd like to take a guess at what happens.
-
4:21
Here we go.
-
4:25
Turns out, it's exactly like when we launched the OpaqueActivity
-
4:29
except OnStop isn't called in MainActivity.
-
4:33
Since we can still see main activity behind our TransparentActivity.
-
4:37
MainActivity is on a pause state and onStop isn't called.
-
4:43
Cool.
-
4:44
For the last test, let's see what happens when we hit the home button.
-
4:49
First, TransparentActivity is pause.
-
4:53
And then, both MainActivity and TransparentActivity are stopped.
-
4:59
When we hit the Home button,
-
5:00
we're telling Android that we're moving on to something else.
-
5:05
Android doesn't know if we'll be back or not, so
-
5:08
it keeps our activities around in a stopped state, in case we return.
-
5:12
Keep in mind that Android may kill stopped activities to free up memory.
-
5:17
You've learned a lot about the activity lifecycle.
-
5:20
In the next video we'll learn how we can hook into our activities lifecycle
-
5:24
to save data using shared preferences.
-
5:27
Also, if you'd like to try these tests on your own, or are curious about how this
-
5:32
app is programmed there's a link to the GitHub project in the teacher's notes.
-
5:36
And it's included below in the project files as well.
You need to sign up for Treehouse in order to download course files.
Sign up