Lifecycle of Fragment: onCreate(), onCreateView() and onPause().

A fragment can be thinked as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them.

Usually, you should implement at least the following lifecycle methods:
  • onCreate()
    The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
  • onCreateView()
    The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
  • onPause()
    The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).


Lifecycle of Fragment

Here, modify from the last exercise "Dynamic change fragment using Java code"; implement(modify) onCreate(), onCreateView() and onPause() methods of MyFragment.java and MyFragment2.java, display a Toast to check the lifecycle of fragment.

MyFragment.java
package com.exercise.FragmentTest;

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 View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragmentlayout, container, false);

Toast.makeText(getActivity(),
"MyFragment.onCreateView()",
Toast.LENGTH_LONG).show();

return myFragmentView;
}

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

Toast.makeText(getActivity(),
"MyFragment.onCreate()",
Toast.LENGTH_LONG).show();
}

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

Toast.makeText(getActivity(),
"MyFragment.onPause()",
Toast.LENGTH_LONG).show();
}

}


MyFragment2.java
package com.exercise.FragmentTest;

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 MyFragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragmentlayout2, container, false);

Toast.makeText(getActivity(),
"MyFragment2.onCreateView()",
Toast.LENGTH_LONG).show();

return myFragmentView;
}

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

Toast.makeText(getActivity(),
"MyFragment2.onCreate()",
Toast.LENGTH_LONG).show();
}

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

Toast.makeText(getActivity(),
"MyFragment2.onPause()",
Toast.LENGTH_LONG).show();
}

}


Download the files.

0 Response to "Lifecycle of Fragment: onCreate(), onCreateView() and onPause()."

Post a Comment