Managing the lifecycle of a fragment is a lot like managing the lifecycle of an activity. Like an activity, a fragment can exist in three states:
Resumed
The fragment is visible in the running activity.
Paused
Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn't cover the entire screen).
Stopped
The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed
Also like an activity, you can retain the state of a fragment using a Bundle, in case the activity's process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment'sonSaveInstanceState() callback and restore it during either onCreate(), onCreateView(), or onActivityCreated().
Coordinating with the activity lifecycle
The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, such that each lifecycle callback for the activity results in a similar callback for each fragment. For example, when the activity receives onPause(), each fragment in the activity receives onPause().
Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the activity in order to perform actions such as build and destroy the fragment's UI. These additional callback methods are:
onAttach()
Called when the fragment has been associated with the activity (the Activity is passed in here).
onCreateView()
Called to create the view hierarchy associated with the fragment.
onActivityCreated()
Called when the activity's onCreate() method has returned.
onDestroyView()
Called when the view hierarchy associated with the fragment is being removed.
onDetach()
Called when the fragment is being disassociated from the activity.
Here i'm giving example app that shows you Fragment Lifecycle call back methods in Logcat:
Application Name:FragmentLifecycle
PackageName:com.ram.fragmentlifecycle
Layout xml files:activity_main.xml,samplefragment.xml
Java Fiels:MainActivity.java,SampleFragment.java
samplefragment.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"
android:background="#ccffcc">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
activity_main.xml:
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/fragment1"
android:name="com.ram.fragmentlifecycle.SampleFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="14dp"
android:layout_marginTop="16dp" />
</RelativeLayout>
SampleFragment.java:
package com.ram.fragmentlifecycle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class SampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d("Fragment Lifecycle", "onCreateView()");
View v = inflater.inflate(R.layout.samplefragment, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.d("Fragment Lifecycle", "onActivityCreated()");
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Log.d("Fragment Lifecycle", "onAttach()");
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("Fragment Lifecycle", "onCreate()");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Fragment Lifecycle", "onDestroy()");
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
Log.d("Fragment Lifecycle", "onDetach()");
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("Fragment Lifecycle", "onPause()");
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("Fragment Lifecycle", "onResume()");
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
Log.d("Fragment Lifecycle", "onSaveInstanceState()");
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d("Fragment Lifecycle", "onStart()");
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("Fragment Lifecycle", "onStop()");
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
Log.d("Fragment Lifecycle", "onDestroyView()");
}
}
MainActivity.java:
package com.ram.fragmentlifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Activity lifecycle", "onCreate()");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
Log.i("Activity lifecycle", "onSaveInstanceState()");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("Activity lifecycle", "onStart()");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("Activity lifecycle", "onStop()");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("Activity lifecycle", "onPause()");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i("Activity lifecycle", "onRestart()");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("Activity lifecycle", "onResume()");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("Activity lifecycle", "onDestroy()");
}
}
Resumed
The fragment is visible in the running activity.
Paused
Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn't cover the entire screen).
Stopped
The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed
Also like an activity, you can retain the state of a fragment using a Bundle, in case the activity's process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment'sonSaveInstanceState() callback and restore it during either onCreate(), onCreateView(), or onActivityCreated().
Coordinating with the activity lifecycle
The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, such that each lifecycle callback for the activity results in a similar callback for each fragment. For example, when the activity receives onPause(), each fragment in the activity receives onPause().
Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the activity in order to perform actions such as build and destroy the fragment's UI. These additional callback methods are:
onAttach()
Called when the fragment has been associated with the activity (the Activity is passed in here).
onCreateView()
Called to create the view hierarchy associated with the fragment.
onActivityCreated()
Called when the activity's onCreate() method has returned.
onDestroyView()
Called when the view hierarchy associated with the fragment is being removed.
onDetach()
Called when the fragment is being disassociated from the activity.
Here i'm giving example app that shows you Fragment Lifecycle call back methods in Logcat:
Application Name:FragmentLifecycle
PackageName:com.ram.fragmentlifecycle
Layout xml files:activity_main.xml,samplefragment.xml
Java Fiels:MainActivity.java,SampleFragment.java
samplefragment.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"
android:background="#ccffcc">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
activity_main.xml:
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/fragment1"
android:name="com.ram.fragmentlifecycle.SampleFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="14dp"
android:layout_marginTop="16dp" />
</RelativeLayout>
SampleFragment.java:
package com.ram.fragmentlifecycle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class SampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d("Fragment Lifecycle", "onCreateView()");
View v = inflater.inflate(R.layout.samplefragment, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.d("Fragment Lifecycle", "onActivityCreated()");
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Log.d("Fragment Lifecycle", "onAttach()");
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("Fragment Lifecycle", "onCreate()");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Fragment Lifecycle", "onDestroy()");
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
Log.d("Fragment Lifecycle", "onDetach()");
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("Fragment Lifecycle", "onPause()");
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d("Fragment Lifecycle", "onResume()");
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
Log.d("Fragment Lifecycle", "onSaveInstanceState()");
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d("Fragment Lifecycle", "onStart()");
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("Fragment Lifecycle", "onStop()");
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
Log.d("Fragment Lifecycle", "onDestroyView()");
}
}
MainActivity.java:
package com.ram.fragmentlifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Activity lifecycle", "onCreate()");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
Log.i("Activity lifecycle", "onSaveInstanceState()");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("Activity lifecycle", "onStart()");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("Activity lifecycle", "onStop()");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("Activity lifecycle", "onPause()");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i("Activity lifecycle", "onRestart()");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("Activity lifecycle", "onResume()");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("Activity lifecycle", "onDestroy()");
}
}
EmoticonEmoticon