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
In this video we will use SharedPreferences in our app to store a user's preference for where s/he wants to store memes: Internal or External storage.
-
0:00
Now that we're feeling more comfortable with shared preferences,
-
0:02
let's introduce this new type of persistence into our application.
-
0:06
You'll remember in the last stage,
-
0:07
we introduced a couple of different ways to store files.
-
0:10
We then implemented a way to verify that reading, writing files worked correctly.
-
0:15
Whether we saved them an internal or external storage.
-
0:18
However, we were forced to make the modifications and then rebuild the app.
-
0:22
This feature might be more suited to a shared preference,
-
0:25
which can be modified by the user at one time.
-
0:27
So, the first thing we'll start with to add shared preferences in
-
0:30
our app is our existing meme maker application settings.
-
0:34
So, let's open up the file now.
-
0:35
This class is currently empty and
-
0:37
will be used as our intermediate layer for handling our shared preferences.
-
0:40
Let's start in here by adding a private variable for shared preferences.
-
0:44
So SharedPreferences, mSharedPreferences.
-
0:51
Again, since everything begins with a context, let's create a constructor for
-
0:55
this class to take in a context.
-
0:58
So we'll do public MemeMakerApplicationSettings passing in
-
1:03
context, with a context.
-
1:07
We don't have a reason to retain the context, so let's just for the time
-
1:10
being create the shared preference object we need with the context.
-
1:13
And save that variable off into the private member field.
-
1:16
So mSharedPreferences equals PreferenceManager,
-
1:22
like we saw before, and then getDefaultSharedPreferences.
-
1:27
And we pass in the context for that.
-
1:29
Next we need a way to get and set these SharedPreference fields.
-
1:33
So, let's create a get storage preference method to return string.
-
1:38
Let's go public String,
-
1:41
getStoragePreference and that will just return.
-
1:47
Now, instead of returning empty, let's add the necessary code to
-
1:50
actually access the shared preferences and get a string out of this storage key.
-
1:55
So, we'll go mSharedPreferences.getString and we'll call it Storage for now.
-
2:03
Let's reuse our storage type constants in this case for the default value.
-
2:08
So, we will default it in that the case has not been set yet
-
2:11
to StorageType.INTERNAL.
-
2:14
And now, we could create the associated set for this preference.
-
2:18
So let's do public, void, setSharedPreference
-
2:26
which will take a parameter of a string and which is it's storageType.
-
2:32
And, at this point, we'll go mSharedPreferences, and
-
2:34
we'll use the fluent interface we talked about before, .edit, then
-
2:40
.putString, and we'll pass in Storage
-
2:46
again here, and then the storageType that's coming in as a parameter.
-
2:52
Then we'll simply commit it.
-
2:55
Instead of using commit, we could also use .apply.
-
2:59
The difference between the two is, commit is synchronous.
-
3:01
Which means it will block execution of this line of code until it
-
3:04
fully saves this field out.
-
3:06
Apply is asynchronous, which means it will do the saving on a separate thread.
-
3:10
Thus allowing execution to continue and not waiting for the save to complete.
-
3:13
One error of caution with using commit is that if you're saving large data sets in
-
3:17
to saved preferences, you could potentially run into a problem where you
-
3:20
can get an application not responding.
-
3:23
So, we have the shared preferences in place at this point.
-
3:25
However, we're not using them in our application.
-
3:27
So, let's head over to the file utilities to integrate this.
-
3:31
If you remember getFileDirectory has a location for
-
3:33
choosing which file storage type to use.
-
3:36
It's currently using a hard-coded field to control this feature.
-
3:40
Let's remove this hard-coded field and
-
3:41
add our Settings class to control the getFileDirectory location.
-
3:47
So, instead of setting this field to String StorageType,
-
3:51
we are going to up above initialize a MemeMakerApplicationSettings.
-
3:56
I'm doing settings equals new MemeMakerApplicationSettings.
-
4:02
Passing and context semi colon.
-
4:06
And then, at this part right here, instead of setting
-
4:11
it to this hard-coded field, we will just say settings.getStoragePreferences.
-
4:19
So this will basically put the control of what file storage in the use of
-
4:23
the hands of the shared preferences.
-
4:25
And since we are defaulting to internal storage,
-
4:27
this is what the application will start using.
-
4:29
Let's run the app now to verify that everything is still working to
-
4:32
how we expected.
-
4:33
[BLANK_AUDIO]
-
4:47
Creating a separate class like we did is advantageous since it
-
4:51
keeps all the shared preferences in one logical unit.
-
4:54
This prevents the usage of a sprinkling implementation details across
-
4:58
the application.
-
4:59
Since, as you may have noticed,
-
5:01
there are a couple details which require consistency when using shared preferences.
-
5:05
Such as the name of the keys, as well as the type to expect.
-
5:09
We'll run into problems if we set a preference based on one name.
-
5:12
And then try to access that same preference but misspell it.
-
5:15
We also can run into problems if we set a preference based on a string.
-
5:19
And attempt to get that same preference based on an integer.
-
5:23
Keeping it in one logical unit will ensure these details are together and
-
5:27
easily maintained if we choose to refactor or modify how we want to use it.
-
5:32
This will also create a good, consistent pattern for
-
5:34
us to use going forward when we decide to add more functionality.
You need to sign up for Treehouse in order to download course files.
Sign up