Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Introduction to SharedPreferences6:36 with Ben Deitch
One way to persist data in Android is by using SharedPreferences. In this video we'll introduce and start implementing SharedPreferences.
Now that we know about the activity lifecycle, 0:00 we can start to learn about saving data in Android. 0:02 Within Android, there are several approaches for us to save our data and 0:05 we cover them in detail in another course on data persistence. 0:09 Of these approaches, the easiest one to use is shared preferences. 0:13 Shared preferences are used to store key value pairs for our apps. 0:17 Some typical uses for a shared preference could be to save a user's name, 0:22 keep track of which theme a user prefers, or 0:26 to store a counter of how many times a user has opened our app. 0:28 Let's explore shared preferences with a new application. 0:33 I'm going to call the app SharedpreferencesApp and then hit Next. 0:38 Hit Next again, pick Empty Activity, hit Next and then click Finish. 0:47 In this app, we're going to create an edit text that saves its value 0:56 even if we completely close the app. 1:00 Let's start by creating an edit text. 1:04 In activity_main.xml, 1:07 which is in the res folder in the layout directory, let's start 1:09 by getting rid of the Hello World text view by clicking it and hitting Delete. 1:15 Then let's add a plain text text field to the top of our app centered horizontally. 1:21 Next, let's click up here to change the layout width to match parent and 1:30 then scroll down over here to make sure we have an ID. 1:35 Great! 1:42 Lastly, let's create a field for 1:44 our edit text inside mainactivity.java. 1:47 We'll write private EditText and we'll call it mEditText. 1:52 Alt Enter to import edit text. 2:02 Then in the bottom of onCreate, we'll type 2:05 mEditText = in parentheses EditText, findviewbyID, 2:10 and the ID of our edit text, which is editText. 2:17 Looks good. 2:24 Before we go on to implementing shared preferences, I'm going to quickly run 2:26 the app so I can showcase a behavior you wouldn't normally expect. 2:30 Here we have the app. 2:36 I'm going to type in some text and then rotate the app. 2:38 [SOUND] Knowing that Android destroys and 2:41 recreates activities when they rotate and 2:45 that we didn't implement anything that would save this value, 2:50 this text should not persist through a rotation. 2:56 So why did it persist through a rotation? 3:01 It turns out that any views, text views, buttons, check boxes, 3:03 etc., that have a unique ID would have their instant state saved and 3:09 restored automatically by Android. 3:14 Cool, but 3:17 if we leave the activity by hitting the back button and 3:22 then open a new activity or a new instance of our app, we get an empty edit text. 3:26 We still need to use shared preferences to persist our data through the activity 3:33 being destroyed. 3:36 Let's start by creating a field for our shared preferences object. 3:38 At the top of the class, type private 3:43 SharedPreferences and we'll call it m SharedPreferences. 3:46 Then and on Create, let's initialize our shared preferences object 3:54 by typing mSharedPreferences = getSharedPreferences. 4:00 We can see that the first parameter is a string called Name. 4:08 This will be the name of our shared preferences file. 4:12 It's usually a good idea to set this to your package name plus .preferences. 4:16 Just make sure that your preferences name will be unique 4:22 among all the SharedPreferences out there. 4:25 The second parameter is an integer, which represents the mode. 4:28 We'll use mode private so 4:33 our shared preferences file can be accessed by only our application. 4:35 Also, all of the other modes are deprecated. 4:40 Now that we know what parameters to use, let's finish creating our object. 4:44 For the first parameter, type PREFS_FILE in 4:49 all caps to let Android Studio know that this is a private static final string. 4:56 And then type ,_Context.MODE_PRIVATE for the mode. 5:03 Next, let's put our cursor back on PREFS_FILE and hit Alt Enter to have 5:10 Android Studio generate the variable, and 5:15 now we can type in the value we talked about earlier. 5:20 The package name, which for 5:23 me is com.teamtreehouse.sharedpreferencesapp and 5:27 ahead.preferences. 5:35 Great, now that we have access to our shared preferences, 5:38 let's store our string. 5:42 All share preferences objects have an editor, and we need to use that editor, 5:45 when we want to add any key value pairs to our shared preferences. 5:50 Let's create a field for 5:55 our shared preferences editor by going to the top of the class and 5:56 typing private SharedPreferences.Editor and we'll call it mEditor. 6:00 Then, let's initialize our field and the bottom of onCreate by typing MEditor 6:11 = mSharedPreferences.edit. 6:18 We're ready to start using this editor but 6:28 let's take a short break and then see how to use this to save and retrieve data. 6:31
You need to sign up for Treehouse in order to download course files.Sign up