Fragment Life Cycle In Android Application

Overview
 
I hope you had a chance to read my last articles, in which I explained about adding a fragment statically and dynamically in an activity. Here are some useful links,
  1. Android Programming - Day One
  2. Android Programming - Day Two
  3. How To Display A Dialog Windows In Android
  4. How To Display A Progress Dialog Window In Android
  5. Intent In Android
  6. Return Data Using Intent Object In Android Applications
  7. Passing Data Using An Intent Object In Android Applications
  8. Fragments In Android Applications
  9. Adding Fragments Dynamically In Android Application

Introduction

 
In this article, you will learn about the life cycle of a fragment in Android Applications.
 
Fragments have their own life cycle like an activity. Life cycle of a fragment enables you to properly save an instance of the fragment when it is destroyed and restore it to its previous state, when it is recreated.
 
Code
  1. package com.example.administrator.myfragmentapp;    
  2. import android.app.Activity;    
  3. import android.app.Fragment;    
  4. import android.os.Bundle;    
  5. import android.util.Log;    
  6. import android.view.LayoutInflater;    
  7. import android.view.Menu;    
  8. import android.view.MenuItem;    
  9. import android.view.View;    
  10. import android.view.ViewGroup;    
  11. public class Fragment1Activity extends Fragment    
  12. {    
  13.     @Override    
  14.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
  15.         Log.d("Fragment Ist""onCreateView");    
  16.         return inflater.inflate(R.layout.activity_fragment1, container, false);    
  17.     }    
  18.     @Override    
  19.     public void onAttach(Activity activity) {    
  20.         super.onAttach(activity);    
  21.         Log.d("Fragment Ist""onAttach");    
  22.     }    
  23.     @Override    
  24.     public void onCreate(Bundle savedInstanceState) {    
  25.         super.onCreate(savedInstanceState);    
  26.         Log.d("Fragment Ist""onCreate");    
  27.     }    
  28.     @Override    
  29.     public void onActivityCreated(Bundle savedInstanceState) {    
  30.         super.onActivityCreated(savedInstanceState);    
  31.         Log.d("Fragment Ist""onActivityCreated");    
  32.     }    
  33.     @Override    
  34.     public void onStart() {    
  35.         super.onStart();    
  36.         Log.d("Fragment Ist""onStart");    
  37.     }    
  38.     @Override    
  39.     public void onResume() {    
  40.         super.onResume();    
  41.         Log.d("Fragment Ist""onResume");    
  42.     }    
  43.     @Override    
  44.     public void onPause() {    
  45.         super.onPause();    
  46.         Log.d("Fragment Ist""onPause");    
  47.     }    
  48.     @Override    
  49.     public void onStop() {    
  50.         super.onStop();    
  51.         Log.d("Fragment Ist""onStop");    
  52.     }    
  53.     @Override    
  54.     public void onDestroyView() {    
  55.         super.onDestroyView();    
  56.         Log.d("Fragment Ist""onDestroyView");    
  57.     }    
  58.     @Override    
  59.     public void onDestroy() {    
  60.         super.onDestroy();    
  61.         Log.d("Fragment Ist""onDestroy");    
  62.     }    
  63.     public void onDetach() {    
  64.         super.onDetach();    
  65.         Log.d("Fragment Ist""onDetach");    
  66.     }    
  67. }   
Result
 
Run the Application by clicking the buttons Shift + F10. Make sure that the emulator is in landscape mode by clicking the button Ctrl + F11. When the Application is loaded on the Emulator, the following is displayed in the LogCat Window:
 
Result
 
Next, change the orientation of the fragment from the landscape to the portrait, the following is displayed in LogCat Window and destroys the current fragment:
 
Result
 
Recreate the fragment by calling again onAttach(), onCreate() etc. methods.
 
Result
 
Explanation
 
Fragments have their own life cycle like activities. When a fragment is being created, it goes through the following states:  
  • onAttach()
  • onCreate()
  • onCreateView()
  • onActivityCreated()
When the fragment becomes visible, it goes through these states:
  • onStart()
  • onResume()
When the fragment goes into the background mode, it goes through these states:
  • onPause()
  • onStop()
When the fragment is destroyed, it goes through these states:
  • onPause()
  • onStop()
  • onDestroyView()
  • onDestroy()
  • onDetach()
Just like an activity, you can restore an instance of a fragment, using a Bundle object, in the following states:
  • onCreate()
  • onCreateView()
  • onActivityCreated()
Here, we see that most of the states experienced by a fragment are similar to those of activities. Few new states are specific to the fragments, which are:.
  • onAttached() – This is called, when the fragment has been associated with the activity.
  • onCreateView() – This is called to create the view for the fragment.
  • onActivityCreated() – This is called, when the activity’s onCreate() method has been returned.
  • onDestroyView() – This is called, when the fragment’s view is being removed.
  • onDetach() – This is called when the fragment is detached from the activity.
There is one main difference between the activity and the fragment. When an activity goes into the background, the activity is placed in the back stack. This allows the activity to be returned when the user clicks the Back button, but in the case of fragments, they are not automatically placed in the back stack when they go into the background. For this, you need to call explicitly the addToBackStack() method during a fragment transaction.
 

Conclusion

 
In the above article, we learned about the life cycle of a fragment. In the next article, I will explain about the interactions between two or more fragments.


Similar Articles