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
In this video we'll see how we can retrieve the instance state of our activity using a bundle and the onRestoreInstanceState method. We'll then use the retrieved instance state to update our UI.
Now that we've successfully
stored our instance state
0:00
all that's left is to retrieve our bundle
when the activity is recreated and
0:03
use that bundle to setup the screen.
0:08
We had a special method to save
the state of the activity, and
0:11
as you might have guessed,
we also have another to restore the state.
0:14
It's called on restore instant state.
0:19
We can retrieve our bundle by using
the on restore instant state method, or
0:22
by using the bundle we get
from the on create method.
0:26
The only difference is that
on the restore instant state
0:30
won't be hauled with a nulled bundle and
on create will.
0:34
So if you use the bundle with on create
just be sure to check that it isn't null.
0:38
Other than that, it typically
doesn't matter which method we use.
0:43
Let's go with onRestoreInstanceState,
0:47
using the same Ctrl+O
shortcut as last time.
0:50
And hit Enter to select it.
1:00
Now that we have access to our saved
instance state bundle, we need to
1:02
extract the fact and color values and
store them in the appropriate fields.
1:06
Similar to the putString and putInt
methods we used in onSave instance state,
1:11
we can use the getString and
getInt methods here
1:17
to retrieve our values from the bundle,
and put them into the appropriate fields.
1:20
We just need to provided
the key as an argument.
1:26
We can also specify a default
value in case a key isn't found,
1:30
by passing in a second argument,
but we don't need to do that here.
1:35
Let's type those out.
1:39
Set mFact =_savedInstanceState.getString.
1:42
And then we'll provide the KEY of
1:50
KEY_FACT.
1:54
And we'll assign to mColor
1:59
SavedInstanceState.getInt and KEY_COLOR.
2:08
Looks good.
2:16
It's not enough to just
populate the fields.
2:18
We also need to update our
views to use these values.
2:21
We can use these same methods
as the on click listener below
2:26
to set their properties.
2:30
I'll just copy them from the on click
listener up into on restore instance state
2:31
to keep things simple.
2:36
We'll need to set the text of our fact,
as well as the background
2:41
color of the application and
the text color of the button.
2:46
Now that we've updated our views
to use the values passed in by
2:53
the saved instance state bundle,
we should be all set, right?
2:57
There's only one way to find out.
3:03
Let's test the app.
3:05
I've already got my emulator up and
running.
3:07
So, all I need to do is
hit the play button.
3:09
Oh, and in case you haven't seen it yet,
3:15
we have a workshop on getting the most
out of your Android emulator.
3:17
Check the teachers notes for a link.
3:22
Here's the app.
3:24
I'm going to click through
a couple fun facts.
3:26
And then rotate the emulator by
using function key, control,
3:31
F11 on Mac or control, F11 on Windows.
3:35
Instead of jumping back to the first
fact with a green background the app now
3:43
remembers where it was, and can keep
its state through a change of rotation.
3:48
Let's click for a couple more fun
facts and then rotate it again.
3:54
Looks good.
4:03
But we still haven't tested what
happens when we rotate the app
4:06
before hitting the Fun Facts button.
4:10
Let's click the back button
to close the app, and
4:13
then reopen it by clicking
it in the app drawer.
4:16
Then we can rotate the app using
the same shortcut as before.
4:21
Function key control F11 on Mac.
4:25
Or Control F11 on Windows.
4:29
Yikes!
4:33
Looks like we found a bug.
4:34
To figure out why this is going on,
let's step through the code.
4:37
Starting at the on create method.
4:43
We set our content view.
4:47
Then we assign our view variables,
and then set up the on click listener.
4:51
Then, when we rotate the app, we trigger
the on save instant state method
4:59
where we add our mFact and
mColor variables to our bundle.
5:05
But, mFact and
mColor haven't been assigned any values
5:10
because the OnClick listener
hasn't been called yet.
5:15
Instead of saving the values we want.
5:24
We're saving whatever happens to be
the default value for our variables.
5:27
Null for strings and zero for integers.
5:31
Then after saving our state
with mFact as null and
5:38
mColor as zero The app re-starts and
on create is called.
5:44
Then on re-store instant state is called.
5:49
We retrieve the values we saved
in the bundle, null and zero.
5:54
And we update our views with our
new mFact and mColor values.
6:01
To summarize the problem,
we're storing mFact and
6:07
mColor in the bundle before we have
assigned them meaningful values.
6:11
An easy way to fix this is to assign mFact
and mColor a value when we declare them.
6:17
Since mFact ultimately comes from the m
facts array in the fact book class,
6:25
we can use the mFacts array to set m fact
6:31
to the first fact that we show in the app,
the ants fact.
6:35
Jumping into the fact book class, tells
us that the ants fact is at index zero.
6:42
Now we can initialize mFact
to the ants fact by typing
6:50
= mFactBook.mFacts[0].
6:55
Assigning a value to
mColor is fairly similar,
7:06
mColor ultimately comes from the mColors
array in the color wheel class.
7:09
Jumping into the color wheel class
we see that the green background
7:15
we want is at index zero, one, two three,
four, five, six, seven, eight.
7:21
Let's initialize mColor
7:29
to the green color by typing
7:34
=mColor wheel.mColors8.
7:39
But, since mColors is an array
of strings and mColor is an int,
7:46
we'll also need to use
the color .parse color
7:53
method to confer the mColor
string end to an end.
7:58
Let's do that.
8:04
Color.parseColor and
8:06
close the parenthesis.
8:11
That should handle the case where
we're handling our instance state
8:16
before it has meaningful values.
8:19
Let's run the app to verify
that we fixed the bug.
8:22
Now when we rotate the app at the
beginning, Everything behaves as expected.
8:27
Nice work.
8:34
You need to sign up for Treehouse in order to download course files.
Sign up