‫Welcome back.
‫In this video we're going to deep dive into the fragments life cycle.
‫Each fragment instance has its own life cycle.
‫When a user navigates and interacts with your app, your fragments transition through various states
‫in their life cycle as they are added or removed, and enter or exit the screen.
‫The fragment class includes callback methods that correspond to each of the changes in the fragments
‫life cycle.
‫For example, on attach on create on create view on activity created.
‫These methods are included in the activity state created.
‫Then we have.
‫On start the activity is visible on resume, the activity running in foreground or resumed active on
‫pose activity partially obscured.
‫Onstop activity not visible.
‫And we have three methods.
‫Callback methods on destroy view on destroy, and on detach.
‫When the activity is destroyed, let's move to Android Studio and with the same app that we created
‫before, we're going to implement our callback methods, let's move to fragment number one locate file.
‫And here before the Oncreate view I'm going to implement various callback methods.
‫At first I'm going to use on attach method.
‫This method is called when the fragment is associated with an activity.
‫The fragment gets a reference to the hosting activity in the context parameter.
‫I'll create a toast message to tell us that we are on attach method.
‫And make sure when using fragments you don't use this.
‫This is referred for activities, but fragments uses the context that it is passed on every callback
‫method.
‫So here I need to pass the parameter context.
‫Passed the unattached method.
‫I created a toast message to tell that we are on attach method again.
‫Guys on attach method is called when the fragment is associated with an activity.
‫The second method is called Oncreate.
‫So I'm going to override Oncreate method.
‫And here this method is called.
‫When the fragment is created, I'm going to create a toast message telling us that we are in the Oncreate
‫method.
‫Initialization of essential components should be done here in the Oncreate function.
‫The third function is the Oncreate view, and this is a very important function in the callback methods
‫of the fragments.
‫This is called to create a view hierarchy associated with a fragment.
‫So inflate the fragments layout and initialize UI components here in the Oncreate view.
‫And this is the most important method used in fragments.
‫Here I'm going to call toast message on create view and this is good.
‫The fourth method is on activity created.
‫You can see it's deprecated called when the hosting activity is Oncreate method has completed interaction
‫with the activity can be done here, but I'm not going to interact with it because it's deprecated.
‫The fifth method is on start method, called when the fragment becomes visible to the user.
‫In the same way, I'm going to display a toast message on start UI updates and resource allocations
‫can be done here.
‫Then we have Onresume method called when the fragment is visible and active.
‫So you can start animations, acquire resources and register listeners.
‫Here I'll copy this test toast message and replace Onstart with the Onresume is called.
‫Another function is on pose called when the fragment is no longer interacting with the user.
‫You can save UI state or stop ongoing operations here in the on pose, on pose on stop.
‫This function is called when the fragment is no longer visible to the user.
‫You can release the resources, stop animations, or save persistent data here in the Onstop method.
‫Then we have Ondestroy view method called when the view hierarchy associated with the fragment is being
‫removed.
‫Clean up resources associated with the UI in the Ondestroy view method on destroy view method is called.
‫Then we have another method called on destroy called when the fragment is being destroyed.
‫So you can perform final cleanup here on destroy.
‫On detach method called when the fragment is being disassociated from the activity.
‫Release references to the activity and other resources here on the on detach method.
‫Okay, remember that these lifecycle methods are called by Android framework, and your implementation
‫in each method should adhere to the specific responsibilities associated with that lifecycle state.
‫Properly managing resources and state is critical for avoiding memory leaks and ensuring a smooth user
‫experience.
‫Now let's run our application and see the order of these callback methods.
‫Execution.
‫And here we go.
‫This is our app.
‫If I click on show fragment number one on attach method is called Oncreate.
‫One is called Oncreate.
‫View method is called on start method is called and on resume method is called.
‫This is for the appearance and visibility of the first fragment.
‫If I click on Show fragment number two or I click on home, you can see on post method is called.
‫Then if I select this app again on start method is called on resume, method is called and the application
‫is visible and the first fragment is visible.
‫If I click on show fragment number two on post method is called on stop method is called on destroy.
‫View method is called and on destroy method is called.
‫Don't miss.
‫The on detach method is also is called because the fragment number one is being destroyed.
‫Void and replaced by fragment number two.
‫If you go back to show fragment number one, all of these methods will be re-executed from unattached
‫to onresume.
‫This is the life cycle of fragments in Android.