Android Fragment Lifecycle Tutorial with Example


                    If you are reading this article then you must be aware about the basic concepts of android application development such as Activity and Fragments. Furthermore if you have been doing android application development for about three weeks or a month maybe, then you must be aware about "Activity Life Cycle". A concept exists similar to it for fragments as well. Understanding Fragment life cycle is very important specifically when you have to develop multiple screen size (say tablets) supporting android applications.
                    The life-cycle of a fragment is connected to the life-cycle of its host activity. Fragments have their own life cycle and it is somewhat similar to the life cycle of an activity but, they have more stages as compared to the activity life-cycle. The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, which means in simple words, if the activity is destroyed then its corresponding fragments will also be destroyed.

There are several methods present in a fragment life cycle, some of which are exactly similar to activity life-cycle. Following is the list of methods which are available:

1. onAttach():
            This method is called when the fragment gets attached to the activity. At this stage both activity and fragment are not initialized. Typically you get in this method, a reference to the activity which uses the fragment for further initialization work.

2. onCreate():
            The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resume it.

3. onCreateView():
            The system calls this when it is time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI. Till this point, activity is not fully initialized so don't interact with activity here.

4. onActivityCreated():
            This is the point where host activity gets created. This is important to notice that this method is called after onCreateView(). This method gets called when the activity's onCreate() method has returned. At this point, view can be accessed with the findViewById() method. Example: In this method you can instantiate objects which require a Context object.

5. onStart():
            This method gets called when the Fragment is visible to the user. This is generally tied to Activity.onStart of the containing Activity's lifecycle.

6. onResume():
            From this point onwards, Fragment becomes active.

7. OnPause(): 
            This method gets called when our fragment is not active anymore (here fragment is visible). This is generally tied to Activity.onPause of the containing Activity's lifecycle. Example: If any other activity takes place of the host activity then this method will be called.

8. onStop():
            This gets called when fragment is no more visible. This is generally tied to the Activity.onStop of the containing Activity's lifecycle.

9. onDestroyView(): 
           This method destroy's the view hierarchy of the fragment. When a fragment comes again into view from backstack then OnCreateView() is called.

10. OnDestroy(): 
           This method does the final cleanup related to the fragment state but is not guaranteed  to get called by the Android platform.

11. OnDetach(): 
          This method gets called when the fragment is being disassociated from the activity.
Note: When overriding these lifecycle methods — with the exception of onCreateView() — you must call through to the super class’s implementation of the method. Otherwise, an exception occurs at run-time.

Fragment Life cycle 

Fragment Life-Cycle

Syntax to override fragment lifecycle methods :
public class LifeCycleFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup parentViewGroup,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_life_cycle, parentViewGroup, false);
        return rootView;

Here I have showed how we can override one of the most commonly used fragment lifecycle method. Similarly other methods can be overridden.

Practical Applications: 
            Fragment lifecycle plays a very crucial role when we are extensively working with tablets where we want a multi pane layout. Here comes fragments into the picture and with them the fragment lifecycle. Thorough understanding of fragment lifecycle is very important if we want to work with multi pane layouts. In most of the popular applications like whatsapp, facebook etc. where fragments is used in form of navigation drawers, multi pane layouts etc.

Write comments
  1. When would you call the server call to get data back, is it from the fragments parent activity or when the fragment is created? Really would appreciate the help.


Popular Posts