1 00:00:00,230 --> 00:00:03,290 Now that we have our editor we can start saving our values. 2 00:00:04,340 --> 00:00:07,790 We learned in a previous video that the on pause life cycle method 3 00:00:07,790 --> 00:00:10,380 is a good place to save any unsaved changes. 4 00:00:11,710 --> 00:00:14,712 Let's make some space at the bottom of our file and 5 00:00:14,712 --> 00:00:18,230 use the Ctrl+O shortcut to override the on pause method. 6 00:00:23,330 --> 00:00:27,910 Then, we can store our string using the putString method of our editor. 7 00:00:28,930 --> 00:00:32,648 It takes two parameters, the key and the value. 8 00:00:32,648 --> 00:00:39,034 Let's type mEditor.putString and 9 00:00:39,034 --> 00:00:45,009 I'll call my key KEY_EDITTEXT. 10 00:00:45,009 --> 00:00:51,191 And for the value, we'll do mEditText.getText, 11 00:00:51,191 --> 00:00:54,578 which returns an editable. 12 00:00:54,578 --> 00:00:59,858 Then .toString, to make it a string. 13 00:01:02,658 --> 00:01:06,280 Then we can use Alt+Enter on the key to create it. 14 00:01:09,500 --> 00:01:16,930 I'll name mine similar to the variable, key_edittext. 15 00:01:16,930 --> 00:01:19,430 Now that we've saved our string to the editor, 16 00:01:19,430 --> 00:01:24,750 we just need to add mEditor.apply 17 00:01:24,750 --> 00:01:29,580 to save our changes to our shared preferences object. 18 00:01:31,610 --> 00:01:35,940 We've now saved the value of our edit text in the on pause method. 19 00:01:35,940 --> 00:01:40,170 All that's left is to retrieve that value in the on create method and 20 00:01:40,170 --> 00:01:42,320 set the text of our edit text to that value. 21 00:01:44,090 --> 00:01:47,300 We can retrieve the value by using the get string method 22 00:01:47,300 --> 00:01:48,590 on a shared preference object. 23 00:01:50,360 --> 00:01:54,860 When retrieving a value from a shared preference we need to provide the key for 24 00:01:54,860 --> 00:02:00,370 the value and a default value to use in case that key isn't found. 25 00:02:00,370 --> 00:02:05,975 In the bottom of our on create, let's retrieve our string by calling 26 00:02:05,975 --> 00:02:13,720 MSharedPreferences.getString and 27 00:02:13,720 --> 00:02:17,120 passing in our KEY_EDITTEXT string for 28 00:02:17,120 --> 00:02:21,780 the key parameter and an empty string for the default value. 29 00:02:24,410 --> 00:02:28,351 Next, let's save this value into a string called editTextString. 30 00:02:37,190 --> 00:02:45,590 Lastly, let's update our edit text by typing mEditText.setText. 31 00:02:48,550 --> 00:02:50,720 And we'll set it to editTextString. 32 00:02:53,440 --> 00:02:56,240 The last step is to test the app to make sure it works as expected. 33 00:03:00,020 --> 00:03:00,900 I'll type in some text. 34 00:03:07,320 --> 00:03:09,121 And close the app by hitting the back button. 35 00:03:20,440 --> 00:03:24,370 And when we reopen the app, our text is still here, perfect. 36 00:03:25,870 --> 00:03:29,990 Before we go on, let's talk about removing shared preference values. 37 00:03:31,060 --> 00:03:32,560 This is especially useful for 38 00:03:32,560 --> 00:03:36,690 testing, or if your users want to reset an app to it's default values. 39 00:03:38,200 --> 00:03:42,380 This can be done by using the clear method of a shared preference editor object. 40 00:03:43,560 --> 00:03:49,520 Also, we can use the remove method, and pass in a key, to remove just one value. 41 00:03:51,290 --> 00:03:53,270 Once you're done removing values, 42 00:03:53,270 --> 00:03:56,110 don't forget to use the apply method to save your changes. 43 00:03:57,460 --> 00:04:01,260 Shared preferences is the easiest way to store data in Android, but 44 00:04:01,260 --> 00:04:05,140 only if that data can easily fit into a key value pair. 45 00:04:05,140 --> 00:04:08,960 If you're interested in how to store more complicated data, like a picture or 46 00:04:08,960 --> 00:04:13,370 a Java file, please check out our videos on data persistence in Android, 47 00:04:13,370 --> 00:04:15,460 which are linked in the teacher's notes. 48 00:04:15,460 --> 00:04:18,860 They also cover shared preferences in a little more detail. 49 00:04:18,860 --> 00:04:21,870 For now, we're going to move onto a practice project 50 00:04:21,870 --> 00:04:23,670 to test everything that you've learned so far.