Initializing a Fragment with Data7:34 with Ben Deitch
In this video we'll see how to initialize a Fragment with an initial value!
Okay, so it's not so 0:00 much that we really want to show a toast in our ViewPager Fragment. 0:01 It's more like our ViewPager fragment needs to be initialized to 0:05 a specific recipe. 0:09 Which means, we need to figure out how to pass that information into our fragment. 0:10 So showing the toast is really more about, 0:15 how do we initialize a fragment with some data. 0:17 Seems simple enough. 0:21 Let's create a constructor for 0:22 our ViewPagerFragment class that takes in an int for the index of the recipe. 0:24 Public, ViewPagerFragment, 0:31 int index, brackets. 0:35 And now let's read the error to see why this doesn't work. 0:39 Avoid non-default constructors and fragments. 0:44 Use a default constructor plus FragmentSetArguments Bundle instead. 0:48 So to summarize, we shouldn't use a custom constructor. 0:55 Instead, we should pass in any arguments by putting them in a bundle and 0:59 using the setArguments method. 1:03 Then we can retrieve our bundle from within the fragment by calling 1:06 getArguments. 1:10 Let's start by getting rid of this constructor. 1:11 Then let's create the key for 1:16 our index parameter that we'll be storing in a bundle. 1:18 Remember, a bundle is just a way to store key value pairs. 1:22 At the top of the class, let's type psfs and hit Enter. 1:26 Then let's name this key KEY_RECIPE_INDEX. 1:32 And let's set it equal to recipe_index. 1:37 Next, over in main activity, we need to create a bundle containing the index. 1:44 Below where we initialize a ViewPager fragment, 1:50 let's create a bundle named bundle. 1:54 And let's set it equal to new Bundle. 2:01 Then let's use the putInt method to add the index to our bundle, 2:06 bundle.putInt. 2:10 And for the key, ViewPagerFragment.KEY_RECIPE_INDEX. 2:15 And for the int we put index. 2:20 Lastly, let's call setArguments on our new view pager fragment and 2:24 pass on our bundle. 2:28 For our last trick, let's cut out this toast and 2:35 paste it into our view fragments on create view method. 2:38 And then let's update the context parameter to be getActivity 2:49 And let's use Alt+enter to create an index variable. 2:55 Finally let's set this variable equal to getArguments to get the bundle. 3:02 And then getInt and pass in our key. 3:09 That should do it. 3:16 Let's test the app and see if we can still see our toasts. 3:17 Looks good, and the back button is working too. 3:27 And if we rotate the app after we click on another recipe, catastrophe. 3:34 Let's go to the log and it looks like we have a class cast exception. 3:44 Because we're trying to cast view_pager fragment to list fragment. 3:52 As you might have guessed. 4:00 This is because we're looking up our list fragment based on the ID 4:02 of the placeholder view group, which might not be holding the list fragment. 4:09 Luckily, instead of looking up our fragments by their placeholder. 4:15 We can look them up by a tag that we give them. 4:19 And to add a tag, all we need to do is provide that tag 4:23 as a third parameter to our add and replace calls. 4:27 Let's start by creating a tag for each fragment at the top of the class. 4:31 Psfs, Enter and let's name this 4:38 one LIST_FRAGMENT_= "List_fragment". 4:43 Then let's use command, or Ctrl+D, to duplicate this line and 4:51 replace LIST with VIEWPAGER. 4:56 And list with ViewPager. 5:01 Next, let's pass in our list fragment tag for the third parameter to our add call. 5:06 And let's pass in our ViewPager fragment tag for 5:16 the third parameter of this call to replace. 5:19 Now that our fragments are tagged, 5:25 we don't need to use findFragmentById anymore. 5:26 And since this is going off of the screen, I'll add a return there so we can see it. 5:30 And instead of using findFragmentById, let's now use findFragmentByTag. 5:36 And pass in our LIST_FRAGMENT tag. 5:44 All right, let's see if that fixed our issue. 5:49 Let's click on a recipe and then rotate the app. 5:57 It worked. 6:02 And we even saw our toast again. 6:03 Which means onCreate view was called, and our argument is still set. 6:05 Nice. 6:09 Now, instead of showing a toast, let's update the action bar 6:11 to show the name of the recipe, instead of the app name. 6:15 Let's switch to our ViewPagerFragmentClass. 6:19 Delete the toast. 6:24 And type getActivity.setTitle and 6:28 pass in Recipes.names and position index. 6:33 We should also set the title back to the app name when we leave. 6:42 So let's override the on stop method. 6:47 Ctrl+O on stop. 6:51 And then below the call to super, let's type getActivit.setTitle. 6:54 And let's pass and 7:01 getResources.getString(R.string.app_name). 7:04 To retrieve the app name string from the string.xml resource file. 7:15 And if we run the app again, 7:20 We can see the action bar update to reflect which recipe we're looking at. 7:27 Awesome. 7:33
You need to sign up for Treehouse in order to download course files.Sign up