Life Cycle of a Fragment in Android

Conventionally, there was only Activities who takes place in communication. For example, when you trigger any action in Activity1 it would take you to Activity2. It used to be the conventional model for navigation before Android 3.0 (API 11). After android 3.0, Fragment was introduced in Android. Fragment helps you to build better user interface with various chunks of Activity. Within same Activity, you can have different fragments, and you can dynamically add or remove them.


Fragment is a behavior or a portion of user interface in an Activity. You can build multi-pane UI and reuse a fragment in multiple activities. It must be part of an activity. Fragment's life-cycle is directly affected by the host-activity's life cycle. 

Structure of a Fragment

In the image, This act as a Activity, but different fragments are used inside an Activity.
There is ListView in Fragment1.
There is ImageView in Fragment2. 
How Fragment works is really flexible. When particular ListView item is selected in the Fragment1, it will show the image attached to it in the ImageView of Fragment2. Also, The good thing is all this is done only in single Activity.  Also, It makes our life easier with their dynamic ability to add and remove them according to user actions.

Conventionally, you have to select an item from Activity1 and it will open an image related to it inside another Activity2. 


Important things to remember :

  • It is a small chunk of User interface. 
  • It has its own life cycle, just like an Activity. 
  • It can process its own events. 
  • It is a dynamic chunk, means you can add or remove fragment according to user actions in a Activity. 
  • It was introduced in Honeycomb API 11.
Real world scenario of using Fragments 


As you can see in the image, there are three Fragments in a Activity. It would change the data according to the selection of particular person name from the listView. If someone selects different name, the fragment will show the details of that selected person. This is the best example of how you can use Fragments in real world. I am pretty sure you will get an clear understanding now about how to use fragment and why you should use them after seeing the image. This will actually make app very organized, flexible, and smooth lightweight. This is the reason why Fragment was introduced. 
 Uses of Fragments 
It provides flexible user interfaces across different screen sizes.
It provides organized tab structure.
It is used to create Actionbar tabs.
It is a dynamic chunk, means you can add or remove fragment according to user actions in a Activity.
How to create a Fragment? 
The image below shows the step you need to follow in order to create a Fragment. 
You should implement at least following life cycle methods in fragment.

onCreate()  : The system calls this when creating fragment. You should initialize your important components of fragment that you want to retain when the fragment is paused or stopped, then resumed.

onCreateView() :  This method defines the layout of the fragment. It draws the UI (User Interface) of fragment. You must return a View from this method that is the root of your fragment's layout.Remember: You must have XML layout file to attach that layout to fragment. This method indicates that this layout will be used for fragment.

States of Fragment :

  • It exists as a Java object : When you say :
                   Fragment fragment1 = new Fragment();
                It actually exists as a Java object. At this point, it is not attached to the Activity in Android.
  • When you provide the layout file and attach it to the activity, It will have two states.
                   1. Being attached to the activity
                   2. Java object
When you run the Activity, it will inflate the layout, it will gain the state of Visible UI on the screen.
 Life cycle of Fragment
Creation : 


Above image shows how each method will be called in sequence. Suppose, an Activity contains a fragment. The methods will be called as shown in the above image.


  • onAttach() : It is called after Fragment is associated with its Activity. It gets a reference to the Activity object which can be use as Context. 
  • onAttachFragment() : It gets called to notify Activity that there was a Fragment which is recently attached to it. 
  • onCreateView() : This is where you need to attach your Layout file to Fragment. In another words, this method will define the appearance of Fragment.You need to provide a layout file to define appearance of it. 
  • onActivityCreated() : This method will be called after execution of onCreate() method of Activity. It indicates that UI of Activity is finished and you are allowed to use UI elements of Fragment and Activity in this method. 
  • onStart() : This method will be called when Activity or Fragment is about to become visible. 
  • onResume() : This means Activity/Fragment has become visible (Now its in resume state). 
Destruction
Whenever user closes the app, methods will be called in following sequence.

    • onPause() : About to pause a Fragment or an Activity. 
    • onSavedInstanceState : You can use this to save information inside a Bundle object. This will be called before destroying a Fragment to save the data which we can use later to restore the Fragment's state. You can do the same with Activity's onSavedInstanceState() method. Fragment's onSavedInstanceState() method will be called first and then Activity's onSavedInstanceState() method. 
    • onStop : This method gets called when an Activity or a Fragment is no longer visible. 
    • onDestroyView() : This method will destroy the View (XML layout file). It means the UI view will not be accessible after this method. The Fragment will stay as java object, but it's view will be destroyed. 
    • onDestroy() : It is called when Fragment not used. It exists as a java object only. Same happens with an Activity, but it gets called after destroying the Fragment's view. 
    • onDetach() : This method will untie the Fragment from an Activity it was associated with.
    Life Cycle of Fragment

     I have explained each method's function detailed above in the tutorial.

    Run this project and in logcat view according to screenshot, you will be able to see how your fragment will go in different methods.

    You can download and import the project into Android studio, you can test it as follows : 


    Have a happy Coding!
    Enjoy!


    Reference :

    Post a Comment

    0 Comments