1 00:00:00,350 --> 00:00:03,030 We've just finished updating our activities layout 2 00:00:03,030 --> 00:00:05,960 to have a placeholder view group for our fragments. 3 00:00:05,960 --> 00:00:07,990 Now let's head over to MainActivity and 4 00:00:07,990 --> 00:00:10,520 use this placeholder to display our new list fragment. 5 00:00:11,820 --> 00:00:15,420 Before we can display our list fragment, it needs to exist. 6 00:00:15,420 --> 00:00:19,010 Let's create a new list fragment variable at the bottom of the onCreate method. 7 00:00:20,210 --> 00:00:21,230 List fragment. 8 00:00:21,230 --> 00:00:24,910 Let's call it fragment = new ListFragment. 9 00:00:26,710 --> 00:00:28,420 Now that we've got our ListFragment, 10 00:00:28,420 --> 00:00:32,760 all we need to do is add this ListFragment to our place holder view group. 11 00:00:32,760 --> 00:00:36,970 Let's start by creating a new fragment manager variable named, fragment manager. 12 00:00:38,140 --> 00:00:39,570 Fragment manager. 13 00:00:39,570 --> 00:00:42,740 And make sure you're getting the one from the Android.app package and 14 00:00:42,740 --> 00:00:44,180 not the one from the support package. 15 00:00:45,680 --> 00:00:50,330 Name it FragmentManager, and then let's set it = getFragmentManager. 16 00:00:51,645 --> 00:00:54,297 [NOISE] The fragment manager is an interface for 17 00:00:54,297 --> 00:00:56,275 interacting with our fragments. 18 00:00:56,275 --> 00:01:00,418 It helps us keep track of our fragments, let's us manage the back stack of our 19 00:01:00,418 --> 00:01:04,310 fragments, and gives us access to the fragment transactions A.P.I. 20 00:01:04,310 --> 00:01:08,430 which allows us to do things like add or remove fragments. 21 00:01:08,430 --> 00:01:13,520 On the next line, let's create a new fragment transaction variable and 22 00:01:13,520 --> 00:01:15,055 let's call it FragmentTransaction. 23 00:01:16,780 --> 00:01:24,018 Then let's set it = fragmentManager.begintransaction. 24 00:01:24,018 --> 00:01:26,080 Then we see a warning 25 00:01:27,280 --> 00:01:30,640 that this transaction should be completed with the commit call. 26 00:01:31,650 --> 00:01:33,280 So let's add two lines below here. 27 00:01:35,820 --> 00:01:43,140 And add that commit call fragmentTransaction.commit. 28 00:01:43,140 --> 00:01:46,390 Fragment transactions are a lot like shared preferences. 29 00:01:46,390 --> 00:01:49,610 Any changes won't be made permanent until there's a call to commit. 30 00:01:50,770 --> 00:01:52,640 Back up on the line we skipped, 31 00:01:52,640 --> 00:01:55,906 let's finally add our list fragment to our placeholder view group. 32 00:01:55,906 --> 00:02:00,816 Let's type fragmentTransaction.add, 33 00:02:00,816 --> 00:02:05,997 and for the first parameter let's pass in our 34 00:02:05,997 --> 00:02:11,466 placeholder as the ID, R.id.placeholder. 35 00:02:13,070 --> 00:02:15,450 And for the second parameter, let's pass in our fragment. 36 00:02:17,510 --> 00:02:18,240 Awesome. 37 00:02:18,240 --> 00:02:20,600 Now let's test the app and see our fragment in action. 38 00:02:25,100 --> 00:02:25,890 Bummer. 39 00:02:25,890 --> 00:02:27,270 An error. 40 00:02:27,270 --> 00:02:27,980 Over in the log. 41 00:02:36,095 --> 00:02:39,360 It looks like we've got an illegal state exception. 42 00:02:39,360 --> 00:02:42,010 The specified child already has a parent. 43 00:02:42,010 --> 00:02:46,190 You must call removeView on the child's parent first. 44 00:02:46,190 --> 00:02:47,890 Okay, so where did this error occur? 45 00:02:51,700 --> 00:02:54,680 Nowhere, great. 46 00:02:54,680 --> 00:02:59,025 This might be a good time to talk about why fragments can sometimes get a bad rap. 47 00:03:00,560 --> 00:03:02,710 There's just a lot of stuff to handle. 48 00:03:02,710 --> 00:03:04,250 On top of fragment managers and 49 00:03:04,250 --> 00:03:08,980 fragment transactions, fragments themselves also have a life cycle. 50 00:03:08,980 --> 00:03:11,530 And the interplay between the fragment life cycle and 51 00:03:11,530 --> 00:03:16,120 the activity life cycle can sometimes leave you scratching your head, like here. 52 00:03:17,440 --> 00:03:18,720 To fix this error. 53 00:03:18,720 --> 00:03:21,020 we need to head over to our list fragment class. 54 00:03:22,770 --> 00:03:26,630 Let's add a third parameter to the inflate method and pass in false. 55 00:03:29,590 --> 00:03:31,620 The third parameter determines whether or 56 00:03:31,620 --> 00:03:36,770 not we attach this view to the view group provided as the second parameter. 57 00:03:36,770 --> 00:03:39,360 It might seem weird that we would set this to false 58 00:03:39,360 --> 00:03:41,910 when we actually do want to attach to that view group. 59 00:03:43,450 --> 00:03:46,080 But when on Create view returns, 60 00:03:46,080 --> 00:03:50,050 our view will be automatically added to the container view group by the system. 61 00:03:51,260 --> 00:03:55,360 So if we don't set this to false, it'll be added twice and 62 00:03:55,360 --> 00:03:56,990 will get the error we saw earlier. 63 00:03:58,830 --> 00:04:01,249 Now that that's fixed, let's run the app again. 64 00:04:06,128 --> 00:04:07,750 And there's our fragment. 65 00:04:07,750 --> 00:04:08,990 Nice. 66 00:04:08,990 --> 00:04:13,230 In the next video, we'll start making our list fragment look a bit more list like.