Hello Guys!!! Hope You all doing well. Today I am going to discuss fragment and its lifecycle methods. In my previous post Fragment and It uses I had discussed the basic of fragments and Its implementation. Today we are moving further to discuss the fragment lifeCycle and its Type.
As we know that fragment lives only inside an Activity that acts as a container.Each fragment has its own view hierarchy that can be inflated as we do usually. A fragment lifecycle is more complex than the activity lifecycle because it has more states. These lifecycle states are shown above.
Now I am going to shade some light on this lifeCycle states.
1. onAttach()
This method is called as soon as the fragment is “attached” to the “father” activity and we can use this method to store the reference about the activity.
2. onCreate()
After calling onAttach() method we have to call onCreate() method. It is one of the most important step, our fragment is in the creation process. This method can be used to start some thread to retrieve data information, maybe from a remote server.
3. onCreateView()
The onCreateView is the method called when the fragment has to create its view hierarchy. During this method we will inflate our layout inside the fragment as we do for example in the ListView widget(Fragment and It uses). During this phase we can’t be sure that our activity is still created so we can’t count on it for some operation.
4. onActivityCreated()
onActivityCreated() called to give a notification to fragment that “father” activity is created and it is ready for use. From now on, our activity is active and created and we can use it when we need.
5. onStart()
The next step is onStart() method. Here we do the common things as in the activity onStart(), during this phase our fragment is visible but it isn’t still interacting with the user.
6. onResume()
When the fragment is ready to interact with user onResume() is called. At the end of this phase our fragment is up and running.
7. onPause()
Then it can happen that the activity is paused and so the activity’s onPause is called. Well onPause fragment method is called too.
8. onDestroyView()
After it it can happen that the OS decides to destroy our fragment view and so onDestroyView is called.
9. onDestroy()
After it, if the system decides to dismiss our fragment it calls onDestroy() method. Here we should release all the connection active and so on because our fragment is close to die.
10. onDetach()
Even if it is during the destroy phase it is still attached to the father activity. The last step is detach the fragment from the activity and it happens when onDetach() is called.
This is all about fragment lifeCycle. Now I am putting all above this as summary.
As we know that fragment lives only inside an Activity that acts as a container.Each fragment has its own view hierarchy that can be inflated as we do usually. A fragment lifecycle is more complex than the activity lifecycle because it has more states. These lifecycle states are shown above.
Now I am going to shade some light on this lifeCycle states.
1. onAttach()
This method is called as soon as the fragment is “attached” to the “father” activity and we can use this method to store the reference about the activity.
2. onCreate()
After calling onAttach() method we have to call onCreate() method. It is one of the most important step, our fragment is in the creation process. This method can be used to start some thread to retrieve data information, maybe from a remote server.
3. onCreateView()
The onCreateView is the method called when the fragment has to create its view hierarchy. During this method we will inflate our layout inside the fragment as we do for example in the ListView widget(Fragment and It uses). During this phase we can’t be sure that our activity is still created so we can’t count on it for some operation.
4. onActivityCreated()
onActivityCreated() called to give a notification to fragment that “father” activity is created and it is ready for use. From now on, our activity is active and created and we can use it when we need.
5. onStart()
The next step is onStart() method. Here we do the common things as in the activity onStart(), during this phase our fragment is visible but it isn’t still interacting with the user.
6. onResume()
When the fragment is ready to interact with user onResume() is called. At the end of this phase our fragment is up and running.
7. onPause()
Then it can happen that the activity is paused and so the activity’s onPause is called. Well onPause fragment method is called too.
8. onDestroyView()
After it it can happen that the OS decides to destroy our fragment view and so onDestroyView is called.
9. onDestroy()
After it, if the system decides to dismiss our fragment it calls onDestroy() method. Here we should release all the connection active and so on because our fragment is close to die.
10. onDetach()
Even if it is during the destroy phase it is still attached to the father activity. The last step is detach the fragment from the activity and it happens when onDetach() is called.
This is all about fragment lifeCycle. Now I am putting all above this as summary.
Phase I: When a fragment gets created, it goes through the following states:
- onAttach()
- onCreate()
- onCreateView()
- onActivityCreated()
Phase II: When the fragment becomes visible, it goes through these states:
- onStart()
- onResume()
Phase III: When the fragment goes into the background mode, it goes through these states:
- onPaused()
- onStop()
Phase IV: When the fragment is destroyed, it goes through the following states:
- onPaused()
- onStop()
- onDestroyView()
- onDestroy()
- onDetach()