articles

Home / DeveloperSection / Articles / Fragment Life cycle in Android

Fragment Life cycle in Android

Manoj Pandey5532 27-Mar-2015

Android fragment is a part of an activity. Fragment is a piece of activity for user interface. A single activity can contain multiple fragments and many fragments can be reused in many, different activities. In Activity we can add or remove fragment when application is running and a fragment can be used multiple activities.

In activity fragment has own life cycle and very similar to an android activity lifecycle. Core life cycle of fragment
1.      onAttach(Activity) called once the fragment is associated with its activity. 
2.      onCreate(Bundle) called to do initial creation of the fragment. 
3.      onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view
hierarchy associated with the fragment. 
4.       onActivityCreated(Bundle) tells the fragment that its activity has completed
its own Activity.onCreate(). 
5.      onViewStateRestored(Bundle) tells the fragment that all of the saved state of
its view hierarchy has been restored. 
6.      onStart() makes the fragment visible to the user (based on its containing
activity being started). 
7.      onResume()   makes the fragment interactive. 
8.      onPause()  is called when fragment is no longer interactive. 
9.      onStop()  is called when fragment is no longer visible. 
10.  onDestroyView()   allows the fragment to clean up resources. 
11.  onDestroy()  allows the fragment to do final clean-up of fragment state. 
12.  onDetach()   It is called immediately prior to the fragment no longer being
associated with its activity. 

 

Fragment Life cycle in Android 

Here I am creating a sample of Fragment life cycle which tell you which method call according to life cycle on using fragment 


1.      create layout file for  fragment1.xml  

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="my fragment" />

 

</LinearLayout>

 

2.      Add a class which extends with fragment class and paste following code

package com.example.demofragment;

 

import android.app.Activity;

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Toast;

 

public class MyFragment extends Fragment {

 

     @Override

     public void onAttach(Activity activity) {

           // TODO Auto-generated method stub

           Toast.makeText(getActivity(), "onAttach", 100).show();

           super.onAttach(activity);

     }

 

     @Override

     public View onCreateView(LayoutInflater inflater, ViewGroup container,

                Bundle savedInstanceState) {

           // TODO Auto-generated method stub

           return inflater.inflate(R.layout.fragment1, container, false);

     }

 

     @Override

     public void onActivityCreated(Bundle savedInstanceState) {

           // TODO Auto-generated method stub

           Toast.makeText(getActivity(), "onCreate", 100).show();

           super.onActivityCreated(savedInstanceState);

     }

 

     @Override

     public void onCreate(Bundle savedInstanceState) {

           // TODO Auto-generated method stub

           super.onCreate(savedInstanceState);

     }

 

     @Override

     public void onDestroy() {

           // TODO Auto-generated method stub

           Toast.makeText(getActivity(), "onDestroy", 100).show();

           super.onDestroy();

     }

 

     @Override

     public void onDestroyView() {

           // TODO Auto-generated method stub

           super.onDestroyView();

     }

 

     @Override

     public void onDetach() {

           // TODO Auto-generated method stub

           Toast.makeText(getActivity(), "onDetach", 100).show();

           super.onDetach();

     }

 

     @Override

     public void onPause() {

           // TODO Auto-generated method stub

           Toast.makeText(getActivity(), "onPause", 100).show();

           super.onPause();

     }

 

     @Override

     public void onResume() {

           // TODO Auto-generated method stub

           super.onResume();

     }

 

     @Override

     public void onStart() {

           // TODO Auto-generated method stub

           super.onStart();

     }

 

     @Override

     public void onStop() {

           // TODO Auto-generated method stub

           super.onStop();

     }

 

}

 

3.      Add fragment in activity_main.xml file 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <fragment

        android:id="@+id/myfragment"

        android:name="com.example.demofragment.MyFragment"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

 

</RelativeLayout>

 4.      No need to add extra code in MainActivity class

 

package com.example.demofragment; 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

 

public class MainActivity extends Activity {

 

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_main);

                }

 

               

}

 

Now run your application

   Fragment Life cycle in Android     Fragment Life cycle in Android

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By