Life Cycle of a Fragment Example

Description

Fragments have their own life cycle.

Example

The following code examines the various states experienced by a fragment.

In the res/layout folder, add a new file and name it fragment1.xml. Populate it with the following:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout/*  ww w.  j  a v a2  s. co m*/
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00FF00"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="This is fragment #1"
    android:textColor="#000000"
    android:textSize="25sp" />
</LinearLayout>

Also in the res/layout folder, add another new file and name it fragment2.xml. Populate it as follows:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout//from   w  ww . j a  v  a  2 s.c  o m
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFE00"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="This is fragment #2"
    android:textColor="#000000"
    android:textSize="25sp" />
</LinearLayout>

In activity_main.xml, add the following code:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="horizontal" >
/* w  w w .  j  ava2s  .  c  o  m*/
    <fragment
        android:name="com.java2s.Fragments.Fragment1"
        android:id="@+id/fragment1"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
    <fragment
        android:name="com.java2s.Fragments.Fragment2"
        android:id="@+id/fragment2"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />

</LinearLayout>

Under the com.java2s.Fragments package name, add two Java class files and name them Fragment1.java and Fragment2.java

Add the following code to Fragment1.java:


/*from   w w w  .  j av a2 s. c  om*/
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;

public class Fragment1 extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {

    Log.d("Fragment 1", "onCreateView");

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment1, container, false);
  }

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.d("Fragment 1", "onAttach");
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("Fragment 1", "onCreate");
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.d("Fragment 1", "onActivityCreated");
  }

  @Override
  public void onStart() {
    super.onStart();
    Log.d("Fragment 1", "onStart");
  }

  @Override
  public void onResume() {
    super.onResume();
    Log.d("Fragment 1", "onResume");
  }

  @Override
  public void onPause() {
    super.onPause();
    Log.d("Fragment 1", "onPause");
  }

  @Override
  public void onStop() {
    super.onStop();
    Log.d("Fragment 1", "onStop");
  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();
    Log.d("Fragment 1", "onDestroyView");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d("Fragment 1", "onDestroy");
  }

  @Override
  public void onDetach() {
    super.onDetach();
    Log.d("Fragment 1", "onDetach");
  }

}

Add the following code to Fragment2.java:


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//from ww  w .j  a v a2  s. c om
public class Fragment2 extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment2, container, false);
  }
}




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView