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