Wednesday, June 27, 2012

Life cycle of Fragment

In order to work with Fragment, Lifecycle of Fragment is a must known issue.

It's a experience to know Lifecycle of Fragment. It display the called lifecycle relate method on screen. Via this experience, you (and me) can know more about it.

Life cycle of Fragment


Create /res/layout/fragmentlayout.xml, the layout of the fragment.
<?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="vertical" >
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="It's Fragment" />
   <ImageView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:src="@drawable/ic_launcher"/>
</LinearLayout>


MyFragment.java, extends Fragment.
package com.exercise.AndroidExFragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  View myFragmentView = inflater.inflate(R.layout.fragmentlayout, container, false);
  updateMyStatus("onCreateView()");
  return myFragmentView;
 }
 
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onActivityCreated(savedInstanceState);
  updateMyStatus("onActivityCreated()");
 }

 @Override
 public void onAttach(Activity activity) {
  // TODO Auto-generated method stub
  super.onAttach(activity);
  updateMyStatus("onAttach()");
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  updateMyStatus("onCreate()");
 }

 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  updateMyStatus("onDestroy()");
 }

 @Override
 public void onDestroyView() {
  // TODO Auto-generated method stub
  super.onDestroyView();
  updateMyStatus("onDestroyView()");
 }

 @Override
 public void onDetach() {
  // TODO Auto-generated method stub
  super.onDetach();
  updateMyStatus("onDetach()");
 }

 @Override
 public void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  updateMyStatus("onPause()");
 }

 @Override
 public void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  updateMyStatus("onResume()");
 }

 @Override
 public void onStart() {
  // TODO Auto-generated method stub
  super.onStart();
  updateMyStatus("onStart()");
 }

 @Override
 public void onStop() {
  // TODO Auto-generated method stub
  super.onStop();
  updateMyStatus("onStop()");
 }

 private void updateMyStatus(String myst){
  ((AndroidExFragmentActivity)getActivity()).updateStatus(" >> MyFragment: " + myst);
 }

}


The main activity, AndroidExFragmentActivity.java.
package com.exercise.AndroidExFragment;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidExFragmentActivity extends Activity {
 
 TextView status;
 String myStatus = "";
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        status = (TextView)findViewById(R.id.status);
        updateMyStatus("onCreate()");
    }

    @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  updateMyStatus("onDestroy()");
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  updateMyStatus("onPause()");
 }

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  updateMyStatus("onResume()");
 }

 @Override
 protected void onStart() {
  // TODO Auto-generated method stub
  super.onStart();
  updateMyStatus("onStart()");
 }

 @Override
 protected void onStop() {
  // TODO Auto-generated method stub
  super.onStop();
  updateMyStatus("onStop()");
 }

 private void updateMyStatus(String myst){
  updateStatus("Activity: " + myst);
 }
    
    public void updateStatus(String st){
     
     if(status == null){
      myStatus += "delay - " + st + "\n";
     }else{
      myStatus += st + "\n";
      status.setText(myStatus);
     }
    }
}


main.xml, the main layout to include our fragment.
<?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" >
    
    <ScrollView 
        android:layout_width="0px"
        android:layout_weight="3"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/status"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#555555"/>
    </ScrollView>

    <fragment
        class="com.exercise.AndroidExFragment.MyFragment"
        android:id="@+id/myfragment"
        android:layout_width="0px"
        android:layout_weight="2"
        android:layout_height="match_parent" />

</LinearLayout>


Download the files.

Related:
- Life cycle of MasterDetailFlow HelloWorld

No comments: