1) Fragment Life Cycle

To make a fragment we have to:

1) Extend the fragment class
2) Give the appearance in XML or Java
3) Override the onCreateView() method to link appearance
4) Use the fragment in XML or Java

CREATING a fragment
Activity cycle 1

1) Activity onCreate() called, where activity can set its view using the setContentView() method.
2) Fragment has it’s onAttach() method called after the fragment is associated with its activity. It gets a reference to the activity object which can be used as context.
3) onAttachFragment() is called by activity to notify the activity that a fragment was attached recently.
4) onCreate() is called inside the fragment

What is the difference between onCreate() in the activity, and onCreate() in the fragment?

  • In activity onCreate(), you use setContentView() and link the appearance, in onCreate() of the fragment you are not supposed to access UI elements of the fragment because the activity onCreate() may not yet be finished,however, you can create background threads here for longer running operations
  • 5) onCreateView() method called in the fragment – this is where you link the layout file of the fragment to its object
    6) onActivityCreated() is called AFTER the activities onCreate() method is completed. So here you can access UI elements through this method.
    7) onStart() is called in the activity
    8) onStart() is called in the fragment
    9) onResume() is called in the activity
    10) onResume() is called in the fragment

    In the blue box, this represents the methods that are called for each fragment. For every fragment 3,4,5 are called and finally 6.

    when a fragment is about to be destroyed
    Activity cycle 2

    onSaveInstanceState() is used to save information about the fragment/activity in the Bundle object
    onDestroyView() is called after the fragment view hierarchy is no longer accessible
    onDestroy() is called after the fragment is not used, it still exists as a java object attached to the activity
    onDetach() – fragment is not tied to the activity, and does not have a view hierarchy

    Demo of fragment lifecycle with code

    MainActivity.java

    package com.jamesfroggatt.fragments1.app;
    
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    
    
    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    
    }
    

    MyFragment.java

    package com.jamesfroggatt.fragments1.app;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    public class MyFragment extends Fragment{
    
    
        @Override
        public void onAttach(Activity activity) {
            Log.i("Fragment ","onAttach");
            super.onAttach(activity);
        }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            Log.i("Fragment ","onCreate");
            super.onCreate(savedInstanceState);
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Log.i("Fragment ","onCreateView");
           // return super.onCreateView(R.layout.james_fragment_layout, container, false);
            return inflater.inflate(R.layout.james_fragment_layout, container, false);
        }
    
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            Log.i("Fragment ","onActivityCreated");
            super.onActivityCreated(savedInstanceState);
    
        }
    
    
        @Override
        public void onStart() {
            Log.i("Fragment ","onStart");
            super.onStart();
        }
    
    
        @Override
        public void onResume() {
            Log.i("Fragment ","onResume");
            super.onResume();
    
        }
    
        @Override
        public void onPause() {
            Log.i("Fragment ","onPause");
            super.onPause();
        }
    
        @Override
        public void onSaveInstanceState(Bundle outState) {
            Log.i("Fragment ","onSaveInstanceState");
            super.onSaveInstanceState(outState);
        }
    
        @Override
        public void onStop() {
            Log.i("Fragment ","onStop");
            super.onStop();
        }
    
    
        @Override
        public void onDestroyView() {
            Log.i("Fragment ","onDestroyView");
            super.onDestroyView();
        }
    
    
        @Override
        public void onDestroy() {
            Log.i("Fragment ","onDestroy");
            super.onDestroy();
        }
    
    
        @Override
        public void onDetach() {
            Log.i("Fragment ","onDetach");
            super.onDetach();
        }
    }
    

    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:background="#ffcc0044"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.jamesfroggatt.fragments1.app.MainActivity">
    
        <TextView
            android:text="@string/hello_world"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <fragment
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:name="com.jamesfroggatt.fragments1.app.MyFragment"
            tools:layout="@layout/james_fragment_layout" />
    
    </RelativeLayout>
    

    james_fragment_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="100dp"
        android:background="#ffccc320"
        >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ff30cc4e"
            android:paddingLeft="50dp"
            android:text="This is MyFragment"/>
    
    </LinearLayout>
    

    Demo of fragment lifecycle and activity lifecycle with code

    MainActivity.java

    package com.jamesfroggatt.fragments1.app;
    
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.util.Log;
    import android.view.Menu;
    
    
    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.e("Activity ","onCreate");
            setContentView(R.layout.activity_main);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            Log.e("Activity ","onStop");
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            Log.e("Activity ","onRestoreInstanceState");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.e("Activity ","onDestroy");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.e("Activity ","onPause");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.e("Activity ","onResume");
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            Log.e("Activity ","onSaveInstanceState");
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Log.e("Activity ","onStart");
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.e("Activity ","onRestart");
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
    
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    
    }
    

    MyFragment.java

    package com.jamesfroggatt.fragments1.app;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    public class MyFragment extends Fragment{
    
    
        @Override
        public void onAttach(Activity activity) {
            Log.i("Fragment ","onAttach");
            super.onAttach(activity);
        }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            Log.i("Fragment ","onCreate");
            super.onCreate(savedInstanceState);
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Log.i("Fragment ","onCreateView");
            // return super.onCreateView(R.layout.james_fragment_layout, container, false);
            return inflater.inflate(R.layout.james_fragment_layout, container, false);
        }
    
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            Log.i("Fragment ","onActivityCreated");
            super.onActivityCreated(savedInstanceState);
    
        }
    
    
        @Override
        public void onStart() {
            Log.i("Fragment ","onStart");
            super.onStart();
        }
    
    
        @Override
        public void onResume() {
            Log.i("Fragment ","onResume");
            super.onResume();
    
        }
    
        @Override
        public void onPause() {
            Log.i("Fragment ","onPause");
            super.onPause();
        }
    
        @Override
        public void onSaveInstanceState(Bundle outState) {
            Log.i("Fragment ","onSaveInstanceState");
            super.onSaveInstanceState(outState);
        }
    
        @Override
        public void onStop() {
            Log.i("Fragment ","onStop");
            super.onStop();
        }
    
    
        @Override
        public void onDestroyView() {
            Log.i("Fragment ","onDestroyView");
            super.onDestroyView();
        }
    
    
        @Override
        public void onDestroy() {
            Log.i("Fragment ","onDestroy");
            super.onDestroy();
        }
    
    
        @Override
        public void onDetach() {
            Log.i("Fragment ","onDetach");
            super.onDetach();
        }
    }
    

    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:background="#ffcc0044"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.jamesfroggatt.fragments1.app.MainActivity">
    
        <TextView
            android:text="@string/hello_world"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <fragment
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:name="com.jamesfroggatt.fragments1.app.MyFragment"
            tools:layout="@layout/james_fragment_layout" />
    
    </RelativeLayout>
    

    james_fragment_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="100dp"
        android:background="#ffccc320"
        >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ff30cc4e"
            android:paddingLeft="50dp"
            android:text="This is MyFragment"/>
    
    </LinearLayout>
    

    Leave a Reply