1 00:00:00,530 --> 00:00:03,350 We've just discovered that when we rotate the app, 2 00:00:03,350 --> 00:00:05,810 we end up with two list fragments. 3 00:00:05,810 --> 00:00:08,980 And what's worse is that if we keep rotating the app, 4 00:00:08,980 --> 00:00:12,180 we get another new list fragment on each rotation. 5 00:00:12,180 --> 00:00:13,980 So why is this happening? 6 00:00:13,980 --> 00:00:16,520 Well, it's actually just Android trying to be 7 00:00:16,520 --> 00:00:19,510 helpful by restoring our fragments automatically. 8 00:00:19,510 --> 00:00:22,340 When we launch the app, we create our list fragment and 9 00:00:22,340 --> 00:00:24,900 we add it to our UI by using the fragment manager. 10 00:00:25,950 --> 00:00:30,940 Then, when we rotate the app, our fragment is automatically restored and 11 00:00:30,940 --> 00:00:32,220 we create a new fragment. 12 00:00:33,890 --> 00:00:36,460 To see this a little better, let's do some debugging. 13 00:00:38,180 --> 00:00:43,870 Let's add a break point to our activities super.onCreate method, 14 00:00:45,310 --> 00:00:46,600 and then hit the Debug button. 15 00:00:51,810 --> 00:00:55,400 After a few seconds, we should be stopped at our break point. 16 00:00:56,440 --> 00:00:56,940 Nice. 17 00:01:00,580 --> 00:01:02,740 Notice that we have a null savedInstanceState. 18 00:01:03,980 --> 00:01:08,390 Now, since we're mostly interested in what happens after we rotate the app, 19 00:01:08,390 --> 00:01:13,980 let's hit the resume program button and then rotate the app. 20 00:01:17,330 --> 00:01:19,860 And now we're back at our breakpoint again except 21 00:01:19,860 --> 00:01:23,590 this time savedInstanceState isn't null. 22 00:01:23,590 --> 00:01:28,150 Let's step into this super.OnCreate method by using this button over here. 23 00:01:30,930 --> 00:01:33,139 Then let's step over this first log call. 24 00:01:35,915 --> 00:01:37,536 And then let's take a look at the log. 25 00:01:42,378 --> 00:01:45,547 So far so good, just the one onCreate message. 26 00:01:47,240 --> 00:01:51,470 Back on the debug tab, let's step over this super call as well 27 00:01:53,310 --> 00:01:55,410 and once that's done, let's check the log again. 28 00:01:57,300 --> 00:02:02,070 This is what I mean when I say that the fragment will be automatically restored. 29 00:02:02,070 --> 00:02:07,080 We didn't do anything and yet here we have onAttach and onCreate for our fragment. 30 00:02:08,310 --> 00:02:13,853 So going back to the debug tab, if we step out of this method 31 00:02:13,853 --> 00:02:20,769 to go back to main activity it becomes pretty clear where the problem is. 32 00:02:24,618 --> 00:02:28,690 We shouldn't be making a new list fragment if we already have one. 33 00:02:30,140 --> 00:02:32,940 Okay, let's go ahead and end this debugging session and 34 00:02:32,940 --> 00:02:34,150 see how we can fix this. 35 00:02:35,720 --> 00:02:40,000 Remember that when we first launched the app, savedInstantState was null. 36 00:02:41,240 --> 00:02:44,290 But when we rotated it, saveInstanceState got a value. 37 00:02:45,290 --> 00:02:50,315 So really all we need to do is say if 38 00:02:50,315 --> 00:02:54,991 savedInstanceState is null. 39 00:02:59,199 --> 00:03:00,270 Then add our fragment. 40 00:03:03,340 --> 00:03:04,201 Let's quickly run this. 41 00:03:10,958 --> 00:03:18,701 And if we rotate the app it looks like it worked and it did work. 42 00:03:18,701 --> 00:03:21,730 This is a perfectly acceptable solution to our problem. 43 00:03:22,870 --> 00:03:26,420 But depending on your app, it might not be the best solution. 44 00:03:26,420 --> 00:03:30,280 What if we needed access to our list fragment instance from another method and 45 00:03:30,280 --> 00:03:30,960 main activity? 46 00:03:32,600 --> 00:03:35,810 We could refactor our list fragment to be a field, but 47 00:03:35,810 --> 00:03:39,480 still this field is only being set when savedInstanceState is null. 48 00:03:40,660 --> 00:03:43,380 To fix this above the if statement 49 00:03:45,510 --> 00:03:49,400 let's create a new list fragment variable named savedFragment. 50 00:03:55,050 --> 00:04:02,605 And set it equal to getFragmentManager.findFrangmentById(R.id- 51 00:04:02,605 --> 00:04:04,876 .placeholder). 52 00:04:04,876 --> 00:04:10,370 Then let's use Alt Enter to cast the returned fragment as a ListFragment. 53 00:04:13,132 --> 00:04:18,540 The findFragmentById method takes in a resource ID and returns a fragment. 54 00:04:18,540 --> 00:04:21,135 If the fragment can't be found it returns null. 55 00:04:22,420 --> 00:04:26,960 The resource ID we pass in should either be the ID of the fragment 56 00:04:26,960 --> 00:04:28,960 if it was added directly to the XML or 57 00:04:28,960 --> 00:04:33,220 the ID of the fragment's placeholder if it was added dynamically. 58 00:04:35,200 --> 00:04:39,323 Now instead of checking if savedInstanceState is null we can instead 59 00:04:39,323 --> 00:04:41,320 check if savedFragment is null. 60 00:04:44,740 --> 00:04:48,160 Which for now makes the code a little bit easier to read. 61 00:04:49,600 --> 00:04:55,470 We look for a ListFragment and if we don't find it, we create and add a new one. 62 00:04:57,350 --> 00:05:00,790 Now, let's do one last test to make sure everything is in good working order. 63 00:05:06,550 --> 00:05:11,309 So if I rotate and rotate and scroll a bunch. 64 00:05:17,268 --> 00:05:19,500 Nice, no double fragments. 65 00:05:19,500 --> 00:05:21,281 And if we look over to the log. 66 00:05:23,530 --> 00:05:27,810 We've only got one fragment on resume call, just like it should be. 67 00:05:29,350 --> 00:05:32,405 Now that we've got our life cycle under control, let's go ahead and 68 00:05:32,405 --> 00:05:34,910 un-extend our logging classes. 69 00:05:34,910 --> 00:05:40,084 Change logging activity back to AppCompatActivity, 70 00:05:40,084 --> 00:05:44,808 and over in ListFragment change LoggingFragment 71 00:05:44,808 --> 00:05:48,973 back to Fragment, and remove the log call. 72 00:05:53,939 --> 00:05:57,710 Then I'm going to delete the two logging classes. 73 00:05:57,710 --> 00:05:59,213 But you should feel free to keep them around. 74 00:06:03,307 --> 00:06:07,435 In the next video we'll start work on launching a new fragment when we receive 75 00:06:07,435 --> 00:06:08,905 a tap on our ListFragment.