Fragments can use to create a dynamic and multi-pane user interface on android application. Fragment can create with the “Fragment” class, which behaves similar to a nested activity that can define its own layout and manage its own lifecycle. Fragment allows developer to break the screen into different section.
The lifecycle of fragment consist of following steps:-
1. onAttach() :This method will be called first, even before onCreate(),which confirms the fragment has been attached to an activity.
2.onCreate(): This method will be called when creating the fragment. It means when a new fragment instance initializes and attaches to the host.
3. onCreateView() : The system calls this callback method when fragment draw user interface for the first time. View component must be returned from this method which is the root of the fragment’s layout.
4. onViewCreated() : This will be called after onCreateView(). This is useful when inheriting the onCreateView() implementation for setting up the adapter.
5. onActivityCreated() :This will be called after onCreate() and onCreateView(), which indicates the activity’s onCreate() has been completed. onActivityCreated() can be used for that initialization of work
6. onStart() : The onStart() method is called once the fragment gets visible .
7. onResume() : This method is called when the Fragment is visible and intractable.
8. onPause() : The system calls this method as the first indication that the user is leaving the fragment.
9. onStop() : Fragment will be stopped by calling onStop().
10. onDestroyView() : It’s called before onDestroy(). This is the equivalent to onCreateView() where we set up the UI. If there are things that are needed to be cleaned up specific to the UI, then that logic can be put up in onDestroyView()
11. onDestroy() : onDestroy() called to do clean-up of the fragment’s state onDetach() : It’s called after onDestroy(), to notify that the fragment has been disassociated from its main activity