Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Fragment Lifecycle7:00 with Ben Deitch
In this video we'll learn all about the Fragment lifecycle!
We've just finished up our list fragment and 0:00 we're ready to move on to the next fragment. 0:02 But before we get to switching up fragments at runtime, 0:04 we should probably take a good look at the Fragment Lifecycle. 0:07 The Fragment lifecycle is composed of eleven methods, but 0:11 we only need to learn about five of them. 0:14 We already know the other six. 0:16 They are the same lifecycle methods as an activity. 0:18 So fragments have onCreate, onStart, onResume, onPause, 0:22 onStop and onDestroy just like an activity. 0:27 These methods will also be called at roughly the same time 0:31 as their activity counterparts. 0:34 So if an activity's onPause is called, each fragment in that activity 0:36 will receive a call to its onPause method as well. 0:41 The other five methods are for handling interactions between the fragment and 0:44 the activity hosting it. 0:48 The first of these five new methods is onAttach(). 0:50 OnAttach() is the very first method in the Fragment Lifecycle and 0:54 is called when the fragment has first been associated with its activity. 0:57 The Git activity method we've been using will return the null, 1:02 if it's called before onAttach(). 1:05 The second method is onCreateView(). 1:08 OnCreateView() is where we setup the view for our fragments. 1:10 We inflate the view, do any required setup and then return it. 1:14 The third method is onActivityCreated(). 1:19 This method is called once the activities onCreate method has returned. 1:22 If there's some work that needs to happen after the activities view has been 1:27 initialized, but before the user sees the activity, that work should be done here. 1:31 The fourth method is onDestroyView(). 1:36 This method is called when the fragments of view is being destroyed. 1:39 If you need to clean up any resources associated with the fragment's view, 1:43 this is the place to do it. 1:47 The fifth new method is onDetach(). 1:48 This method is called when the fragment is being removed from the activity. 1:51 After onDetach returns, any calls to Git activity will return null. 1:56 And there you have it, all eleven Fragment Lifecycle methods. 2:00 But before we move on, let's quickly run a test to see how the Fragment Lifecycle 2:04 interacts with the activity lifecycle. 2:08 In order to test this, I've created two new classes, LoggingFragment and 2:12 LoggingActivity. 2:17 LoggingFragment will log a message per each Fragment Lifecycle method and 2:18 LoggingActivity will log two messages for each activity lifecycle method. 2:23 One message before the call the super and one message after. 2:28 These classes can be found in the teacher's notes below. 2:32 They're not required for the finished app. 2:35 But if you'd like to follow along in the next two videos, I recommend adding them, 2:37 then let's change MainActivity to extend LoggingActivity. 2:42 And ListFragment to extend the LoggingFragment. 2:53 Next, since we don't call super in our fragments on CreateView method, 3:00 we aren't able to log this method just by extending LoggingFragment. 3:05 So, let's add a log statement right here. 3:09 Log.d and let's use LoggingFragments tagged to make this log 3:12 have the same tag as the other Fragment Lifecycle methods. 3:18 LoggingFragment.TAG and then let's pass in onCreateView, as a string. 3:23 Now when we run the app, 3:34 we should be able to clearly see when each lifecycle method is called. 3:35 So without further ado, let's run it. 3:40 And then let's add a filter to the word on to help narrow down the output and 3:50 also change it to debug. 3:56 And if you're still not seeing anything like me, 3:59 then change this to No Filters and there it is. 4:03 As you probably expected, 4:07 the first method to be called is the activities onCreate method. 4:09 And since we can see both pre and pos-tlog messages, 4:14 we know that on create has been called in its entirety. 4:17 Next we see onAttach, onCreate, onCreateView and 4:21 onActivityCreated from the fragment. 4:25 And if we take a minute to think about this, it makes perfect sense. 4:28 In order to attach a fragment to an activity, that activity must exist. 4:32 So the fragments onAttach method needs to come after the activity's onCreate method. 4:38 On the other end, remember that when an activity returns from onStart, 4:44 it's ready to be displayed to the user. 4:47 But before the activity can be ready to display, 4:50 any fragments in that activity must be created. 4:53 This is why these four fragment methods have to come before onStart. 4:56 On the other end, remember that when an activity returns from onStart, 5:02 it's ready to be displayed to the user. 5:06 But before the activity can be ready to display, 5:09 any fragments in that activity must be ready to display as well. 5:12 So, we've got the activities onCreate method followed by four 5:17 fragment methods and then the activities onStart method. 5:21 Then after the activities onStart method, we have the fragments onStart method and 5:25 we have the activities onResume and then the fragments onResume. 5:30 And then at this point, the app is running. 5:35 Now, if I leave the app by pressing the Back button. 5:38 We see onPause followed by onStop, 5:46 then we see onDestroyView, onDestroy and onDetach for 5:49 our fragment followed by the activity is onDestroy method. 5:54 Cool. 6:01 And one last thing. 6:02 What happens if we open this backup and then rotate it? 6:05 It looks like we've got the same onPause, onStop and 6:15 onDestroy calls just like last time, but 6:19 now it looks like our fragment was attached and created during our 6:23 activities onCreate method and it looks like it was attached and 6:28 created again right after the activity was created. 6:33 Then at the bottom, we've got two calls to our fragments onResume method. 6:39 It looks like we've got two fragments and 6:46 if we scroll the list, we've definitely got two fragments. 6:50 In the next video, we'll see why this is and what we can do to fix it. 6:56
You need to sign up for Treehouse in order to download course files.Sign up