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
What good are SharedPreferences if we don't use them? In this video we'll see how to save and retrieve the data we store in SharedPreferences as well as briefly touch on a couple of other important methods.
Now that we have our editor we
can start saving our values.
0:00
We learned in a previous video that
the on pause life cycle method
0:04
is a good place to save
any unsaved changes.
0:07
Let's make some space at
the bottom of our file and
0:11
use the Ctrl+O shortcut to
override the on pause method.
0:14
Then, we can store our string using
the putString method of our editor.
0:23
It takes two parameters,
the key and the value.
0:28
Let's type mEditor.putString and
0:32
I'll call my key KEY_EDITTEXT.
0:39
And for the value,
we'll do mEditText.getText,
0:45
which returns an editable.
0:51
Then .toString, to make it a string.
0:54
Then we can use Alt+Enter
on the key to create it.
1:02
I'll name mine similar to the variable,
key_edittext.
1:09
Now that we've saved our
string to the editor,
1:16
we just need to add mEditor.apply
1:19
to save our changes to our
shared preferences object.
1:24
We've now saved the value of our
edit text in the on pause method.
1:31
All that's left is to retrieve that
value in the on create method and
1:35
set the text of our edit
text to that value.
1:40
We can retrieve the value by
using the get string method
1:44
on a shared preference object.
1:47
When retrieving a value from a shared
preference we need to provide the key for
1:50
the value and a default value to
use in case that key isn't found.
1:54
In the bottom of our on create,
let's retrieve our string by calling
2:00
MSharedPreferences.getString and
2:05
passing in our KEY_EDITTEXT string for
2:13
the key parameter and
an empty string for the default value.
2:17
Next, let's save this value into
a string called editTextString.
2:24
Lastly, let's update our edit
text by typing mEditText.setText.
2:37
And we'll set it to editTextString.
2:48
The last step is to test the app
to make sure it works as expected.
2:53
I'll type in some text.
3:00
And close the app by
hitting the back button.
3:07
And when we reopen the app,
our text is still here, perfect.
3:20
Before we go on, let's talk about
removing shared preference values.
3:25
This is especially useful for
3:31
testing, or if your users want to
reset an app to it's default values.
3:32
This can be done by using the clear method
of a shared preference editor object.
3:38
Also, we can use the remove method, and
pass in a key, to remove just one value.
3:43
Once you're done removing values,
3:51
don't forget to use the apply
method to save your changes.
3:53
Shared preferences is the easiest
way to store data in Android, but
3:57
only if that data can easily
fit into a key value pair.
4:01
If you're interested in how to store
more complicated data, like a picture or
4:05
a Java file, please check out our
videos on data persistence in Android,
4:08
which are linked in the teacher's notes.
4:13
They also cover shared preferences
in a little more detail.
4:15
For now, we're going to move
onto a practice project
4:18
to test everything that you've learned so
far.
4:21
You need to sign up for Treehouse in order to download course files.
Sign up