1 00:00:00,180 --> 00:00:04,290 Before we start coding, I'd like to talk briefly about our tools. 2 00:00:04,290 --> 00:00:07,950 Remember, the Android Studio is constantly evolving, so 3 00:00:07,950 --> 00:00:12,430 things might be a little different between these videos and what you're using. 4 00:00:12,430 --> 00:00:14,980 If you want things to be exactly the same, 5 00:00:14,980 --> 00:00:18,910 I've provided snapshots of the tools I'm using as downloads in the teacher's notes. 6 00:00:20,010 --> 00:00:24,330 If you run into any problems using the newer version, don't hesitate to ask for 7 00:00:24,330 --> 00:00:26,450 help from the Treehouse community. 8 00:00:26,450 --> 00:00:29,840 Or come back and reinstall these versions of the tools. 9 00:00:29,840 --> 00:00:33,740 As always, though the tools might change, the code, and 10 00:00:33,740 --> 00:00:36,130 concepts, should stay relatively the same. 11 00:00:37,350 --> 00:00:43,170 If any minor changes or bugs pop up, then we'll add a visual call-out like this. 12 00:00:43,170 --> 00:00:47,370 Keep your eyes on the teacher's notes for any known issues or helpful comments. 13 00:00:47,370 --> 00:00:51,060 And if you spot a difference somewhere, check the notes first, and 14 00:00:51,060 --> 00:00:52,340 then let us know if we've missed it. 15 00:00:53,470 --> 00:00:57,440 Before we get started, if you haven't completed the fun facts app, 16 00:00:57,440 --> 00:00:59,870 you may need to download the project files we'll be using. 17 00:01:01,010 --> 00:01:04,970 You can download the project files from the teacher's links below, and 18 00:01:04,970 --> 00:01:08,410 then to import them, go to file, open, and 19 00:01:08,410 --> 00:01:10,700 navigate to the unzipped folder from the download. 20 00:01:12,370 --> 00:01:14,350 If you're comfortable using GitHub, 21 00:01:14,350 --> 00:01:17,490 you can also use the GitHub link below to import the project. 22 00:01:19,100 --> 00:01:22,630 There's also a workshop link below if you'd like to learn more about 23 00:01:22,630 --> 00:01:24,040 Android Studio and GitHub. 24 00:01:25,210 --> 00:01:29,180 Also, remember that you'll get the most from this course, and 25 00:01:29,180 --> 00:01:32,320 all of our material here at Treehouse if you follow along with me. 26 00:01:33,590 --> 00:01:36,530 So, let's fire up the project and get to it. 27 00:01:36,530 --> 00:01:38,710 So, how are we going to fix our activity? 28 00:01:40,010 --> 00:01:42,280 Let's open up the fun facts activity and 29 00:01:42,280 --> 00:01:45,390 go over how we can handle orientation changes the little better. 30 00:01:46,570 --> 00:01:50,110 The first method we need to know is on save instant state. 31 00:01:51,260 --> 00:01:54,110 Let's override the on save instant state method 32 00:01:54,110 --> 00:01:57,080 by using the control O keyboard shortcut. 33 00:01:57,080 --> 00:01:59,870 This lets us pick methods we'd like to override, and 34 00:01:59,870 --> 00:02:02,270 Android Studio will take care of the rest. 35 00:02:02,270 --> 00:02:08,190 Then, let's type onSaveInstanceState to select it. 36 00:02:09,730 --> 00:02:11,440 And hit enter to override the method. 37 00:02:13,255 --> 00:02:16,886 OnSaveInstanceState isn't technically a life cycle method, but 38 00:02:16,886 --> 00:02:20,655 it does provide us with a bundle where we can store the instance state. 39 00:02:23,758 --> 00:02:26,810 We've only used bundles briefly in earlier projects. 40 00:02:26,810 --> 00:02:31,240 But remember that a bundle is a way to pass data between activities 41 00:02:31,240 --> 00:02:32,550 using key value pairs. 42 00:02:34,040 --> 00:02:38,510 Before we go on, let's take a moment to talk about instance state. 43 00:02:38,510 --> 00:02:41,760 Each time we open an Android app that isn't already running, 44 00:02:41,760 --> 00:02:43,630 we get a new instance. 45 00:02:43,630 --> 00:02:45,030 And as we use the app, 46 00:02:45,030 --> 00:02:48,220 we typically make several changes to the state of that instance. 47 00:02:49,390 --> 00:02:53,370 For example, changing the background color in the Fun Facts app, or 48 00:02:53,370 --> 00:02:56,220 navigating to the settings page in an e-mail app. 49 00:02:56,220 --> 00:02:59,210 These aren't changes that we want to permanently save. 50 00:02:59,210 --> 00:03:02,533 We wouldn't want to be greeting with the settings page of 51 00:03:02,533 --> 00:03:05,996 an app right from the start but since Android can destroy our 52 00:03:05,996 --> 00:03:10,567 activities when they're in the background or whenever we rotate our device, 53 00:03:10,567 --> 00:03:13,683 [SOUND] we should save and restore our instance state so 54 00:03:13,683 --> 00:03:17,867 that it all seems like one instance to the user, [SOUND] start to finish. 55 00:03:17,867 --> 00:03:21,630 After all, it wasn't the user who decided to destroy the activity. 56 00:03:22,820 --> 00:03:24,480 Back to the code. 57 00:03:24,480 --> 00:03:28,700 Now that we have our bundle, we can use the put string method to store our fact. 58 00:03:29,830 --> 00:03:31,714 I'll type it out, and then explain it. 59 00:03:46,570 --> 00:03:52,020 The PutString method requires us to provide both the key, and the value. 60 00:03:53,510 --> 00:03:55,910 But we haven't created the key yet. 61 00:03:55,910 --> 00:04:00,550 And we also don't have a fact variable that we can access inside this method. 62 00:04:02,140 --> 00:04:07,360 Luckily, with Android Studio, we can just pretend that they exist, 63 00:04:07,360 --> 00:04:11,790 and then use the quick fix with Alt+Enter to create them later. 64 00:04:11,790 --> 00:04:15,950 It's the best practice for keys to be static final variables. 65 00:04:15,950 --> 00:04:20,880 And further, for static final variables to be written in all caps. 66 00:04:20,880 --> 00:04:25,090 Writing the key in caps, let's Android Studio know what to create for us. 67 00:04:26,920 --> 00:04:32,360 Let's hit Alt+Enter on our key and pick create constant field. 68 00:04:34,200 --> 00:04:38,150 Constant means it will be a static final variable. 69 00:04:38,150 --> 00:04:40,540 And field means it belongs to the class. 70 00:04:41,850 --> 00:04:49,550 Then let's give our key a value of key effect just like the name of the variable. 71 00:04:52,490 --> 00:04:56,448 Back in the on save instant state method, we can do the same with mFact. 72 00:04:56,448 --> 00:05:00,715 Hit Alt+Enter, and 73 00:05:00,715 --> 00:05:05,280 pick field because we want this to be scoped to the class and not the method. 74 00:05:08,070 --> 00:05:12,990 Now that we have our in fact field, we should use it and the on click listener 75 00:05:15,510 --> 00:05:18,990 instead of creating a new string variable in each click. 76 00:05:20,280 --> 00:05:25,016 We'll do that by getting rid of the fact variable and replacing it with mFact. 77 00:05:30,898 --> 00:05:32,490 I'll just paste it right over. 78 00:05:37,320 --> 00:05:38,310 Looks good. 79 00:05:39,600 --> 00:05:42,720 We should probably save the background color in our bundle as well. 80 00:05:43,920 --> 00:05:47,260 The user might notice it changing back to green on every rotation. 81 00:05:48,500 --> 00:05:49,070 Let's do that. 82 00:05:50,330 --> 00:05:56,237 The only difference is, instead of using PutString, we should use putInt. 83 00:05:56,237 --> 00:06:02,816 At the bottom of onSaveInstanceState, 84 00:06:02,816 --> 00:06:10,760 let's add outstate.putInt key color M color. 85 00:06:10,760 --> 00:06:14,390 And then use Alt+Enter to create the variables. 86 00:06:14,390 --> 00:06:19,358 I'll call the key key color. 87 00:06:21,980 --> 00:06:24,490 Create mColor as a field. 88 00:06:29,330 --> 00:06:32,071 And then replace the color variable and 89 00:06:32,071 --> 00:06:35,484 the on click listener with our new mColor field. 90 00:06:43,924 --> 00:06:48,504 All right, we've successfully saved the state of the activity or 91 00:06:48,504 --> 00:06:54,030 how it looks to the user using a bundle and the new method onSaveInstanceState. 92 00:06:54,030 --> 00:06:55,340 Take a short break and 93 00:06:55,340 --> 00:06:59,150 then we can see how to retrieve this state when the activity is created again.