Adding Fragments to Activities4:13 with Ben Deitch
In this video we'll learn how to display our Fragment inside the placeholder ViewGroup!
We've just finished updating our activities layout 0:00 to have a placeholder view group for our fragments. 0:03 Now let's head over to MainActivity and 0:05 use this placeholder to display our new list fragment. 0:07 Before we can display our list fragment, it needs to exist. 0:11 Let's create a new list fragment variable at the bottom of the onCreate method. 0:15 List fragment. 0:20 Let's call it fragment = new ListFragment. 0:21 Now that we've got our ListFragment, 0:26 all we need to do is add this ListFragment to our place holder view group. 0:28 Let's start by creating a new fragment manager variable named, fragment manager. 0:32 Fragment manager. 0:38 And make sure you're getting the one from the Android.app package and 0:39 not the one from the support package. 0:42 Name it FragmentManager, and then let's set it = getFragmentManager. 0:45 [NOISE] The fragment manager is an interface for 0:51 interacting with our fragments. 0:54 It helps us keep track of our fragments, let's us manage the back stack of our 0:56 fragments, and gives us access to the fragment transactions A.P.I. 1:00 which allows us to do things like add or remove fragments. 1:04 On the next line, let's create a new fragment transaction variable and 1:08 let's call it FragmentTransaction. 1:13 Then let's set it = fragmentManager.begintransaction. 1:16 Then we see a warning 1:24 that this transaction should be completed with the commit call. 1:27 So let's add two lines below here. 1:31 And add that commit call fragmentTransaction.commit. 1:35 Fragment transactions are a lot like shared preferences. 1:43 Any changes won't be made permanent until there's a call to commit. 1:46 Back up on the line we skipped, 1:50 let's finally add our list fragment to our placeholder view group. 1:52 Let's type fragmentTransaction.add, 1:55 and for the first parameter let's pass in our 2:00 placeholder as the ID, R.id.placeholder. 2:05 And for the second parameter, let's pass in our fragment. 2:13 Awesome. 2:17 Now let's test the app and see our fragment in action. 2:18 Bummer. 2:25 An error. 2:25 Over in the log. 2:27 It looks like we've got an illegal state exception. 2:36 The specified child already has a parent. 2:39 You must call removeView on the child's parent first. 2:42 Okay, so where did this error occur? 2:46 Nowhere, great. 2:51 This might be a good time to talk about why fragments can sometimes get a bad rap. 2:54 There's just a lot of stuff to handle. 3:00 On top of fragment managers and 3:02 fragment transactions, fragments themselves also have a life cycle. 3:04 And the interplay between the fragment life cycle and 3:08 the activity life cycle can sometimes leave you scratching your head, like here. 3:11 To fix this error. 3:17 we need to head over to our list fragment class. 3:18 Let's add a third parameter to the inflate method and pass in false. 3:22 The third parameter determines whether or 3:29 not we attach this view to the view group provided as the second parameter. 3:31 It might seem weird that we would set this to false 3:36 when we actually do want to attach to that view group. 3:39 But when on Create view returns, 3:43 our view will be automatically added to the container view group by the system. 3:46 So if we don't set this to false, it'll be added twice and 3:51 will get the error we saw earlier. 3:55 Now that that's fixed, let's run the app again. 3:58 And there's our fragment. 4:06 Nice. 4:07 In the next video, we'll start making our list fragment look a bit more list like. 4:08
You need to sign up for Treehouse in order to download course files.Sign up