This course will be retired on July 14, 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
Well done!
You have completed Android Activity Lifecycle!
You have completed Android Activity Lifecycle!
Preview
In this video we'll see how we can save the instance state of our activity using a bundle and the onSaveInstanceState method.
GitHub Link to Project
Related Links
Downloads
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
Before we start coding,
I'd like to talk briefly about our tools.
0:00
Remember, the Android Studio
is constantly evolving, so
0:04
things might be a little different between
these videos and what you're using.
0:07
If you want things to be exactly the same,
0:12
I've provided snapshots of the tools I'm
using as downloads in the teacher's notes.
0:14
If you run into any problems using the
newer version, don't hesitate to ask for
0:20
help from the Treehouse community.
0:24
Or come back and
reinstall these versions of the tools.
0:26
As always, though the tools might change,
the code, and
0:29
concepts, should stay relatively the same.
0:33
If any minor changes or bugs pop up, then
we'll add a visual call-out like this.
0:37
Keep your eyes on the teacher's notes for
any known issues or helpful comments.
0:43
And if you spot a difference somewhere,
check the notes first, and
0:47
then let us know if we've missed it.
0:51
Before we get started, if you
haven't completed the fun facts app,
0:53
you may need to download
the project files we'll be using.
0:57
You can download the project files
from the teacher's links below, and
1:01
then to import them, go to file, open, and
1:04
navigate to the unzipped
folder from the download.
1:08
If you're comfortable using GitHub,
1:12
you can also use the GitHub link
below to import the project.
1:14
There's also a workshop link below
if you'd like to learn more about
1:19
Android Studio and GitHub.
1:22
Also, remember that you'll get
the most from this course, and
1:25
all of our material here at Treehouse
if you follow along with me.
1:29
So, let's fire up the project and
get to it.
1:33
So, how are we going to fix our activity?
1:36
Let's open up the fun facts activity and
1:40
go over how we can handle orientation
changes the little better.
1:42
The first method we need to
know is on save instant state.
1:46
Let's override the on
save instant state method
1:51
by using the control O keyboard shortcut.
1:54
This lets us pick methods
we'd like to override, and
1:57
Android Studio will take care of the rest.
1:59
Then, let's type
onSaveInstanceState to select it.
2:02
And hit enter to override the method.
2:09
OnSaveInstanceState isn't
technically a life cycle method, but
2:13
it does provide us with a bundle where
we can store the instance state.
2:16
We've only used bundles
briefly in earlier projects.
2:23
But remember that a bundle is a way
to pass data between activities
2:26
using key value pairs.
2:31
Before we go on, let's take a moment
to talk about instance state.
2:34
Each time we open an Android
app that isn't already running,
2:38
we get a new instance.
2:41
And as we use the app,
2:43
we typically make several changes
to the state of that instance.
2:45
For example, changing the background
color in the Fun Facts app, or
2:49
navigating to the settings
page in an e-mail app.
2:53
These aren't changes that we
want to permanently save.
2:56
We wouldn't want to be greeting
with the settings page of
2:59
an app right from the start but
since Android can destroy our
3:02
activities when they're in the background
or whenever we rotate our device,
3:05
[SOUND] we should save and
restore our instance state so
3:10
that it all seems like one instance
to the user, [SOUND] start to finish.
3:13
After all, it wasn't the user who
decided to destroy the activity.
3:17
Back to the code.
3:22
Now that we have our bundle, we can use
the put string method to store our fact.
3:24
I'll type it out, and then explain it.
3:29
The PutString method requires us to
provide both the key, and the value.
3:46
But we haven't created the key yet.
3:53
And we also don't have a fact variable
that we can access inside this method.
3:55
Luckily, with Android Studio,
we can just pretend that they exist,
4:02
and then use the quick fix with
Alt+Enter to create them later.
4:07
It's the best practice for
keys to be static final variables.
4:11
And further, for static final
variables to be written in all caps.
4:15
Writing the key in caps, let's
Android Studio know what to create for us.
4:20
Let's hit Alt+Enter on our key and
pick create constant field.
4:26
Constant means it will be
a static final variable.
4:34
And field means it belongs to the class.
4:38
Then let's give our key a value of key
effect just like the name of the variable.
4:41
Back in the on save instant state method,
we can do the same with mFact.
4:52
Hit Alt+Enter, and
4:56
pick field because we want this to be
scoped to the class and not the method.
5:00
Now that we have our in fact field,
we should use it and the on click listener
5:08
instead of creating a new
string variable in each click.
5:15
We'll do that by getting rid of the fact
variable and replacing it with mFact.
5:20
I'll just paste it right over.
5:30
Looks good.
5:37
We should probably save the background
color in our bundle as well.
5:39
The user might notice it changing
back to green on every rotation.
5:43
Let's do that.
5:48
The only difference is, instead of
using PutString, we should use putInt.
5:50
At the bottom of onSaveInstanceState,
5:56
let's add outstate.putInt
key color M color.
6:02
And then use Alt+Enter
to create the variables.
6:10
I'll call the key key color.
6:14
Create mColor as a field.
6:21
And then replace the color variable and
6:29
the on click listener with
our new mColor field.
6:32
All right, we've successfully
saved the state of the activity or
6:43
how it looks to the user using a bundle
and the new method onSaveInstanceState.
6:48
Take a short break and
6:54
then we can see how to retrieve this
state when the activity is created again.
6:55
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