This code snippet is taken from Professional Android 4 Application Development

The example shows skeleton code for the fragment lifecycle event handlers.

public class MySkeletonFragment extends Fragment {

  // Called when the Fragment is attached to its parent Activity.
  public void onAttach(Activity activity) {
    // Get a reference to the parent Activity.

  // Called to do the initial creation of the Fragment.
  public void onCreate(Bundle savedInstanceState) {
    // Initialize the Fragment.

  // Called once the Fragment has been created in order for it to
  // create its user interface.
  public View onCreateView(LayoutInflater inflater, 
                           ViewGroup container,
                           Bundle savedInstanceState) {
    // Create, or inflate the Fragment's UI, and return it. 
    // If this Fragment has no UI then return null.
    return inflater.inflate(R.layout.my_fragment, container, false);

  // Called once the parent Activity and the Fragment's UI have 
  // been created.
  public void onActivityCreated(Bundle savedInstanceState) {
    // Complete the Fragment initialization – particularly anything
    // that requires the parent Activity to be initialized or the 
    // Fragment's view to be fully inflated.

  // Called at the start of the visible lifetime.
  public void onStart(){
    // Apply any required UI change now that the Fragment is visible.

  // Called at the start of the active lifetime.
  public void onResume(){
    // Resume any paused UI updates, threads, or processes required
    // by the Fragment but suspended when it became inactive.

  // Called at the end of the active lifetime.
  public void onPause(){
    // Suspend UI updates, threads, or CPU intensive processes
    // that don't need to be updated when the Activity isn't
    // the active foreground activity.
    // Persist all edits or state changes
    // as after this call the process is likely to be killed.

  // Called to save UI state changes at the
  // end of the active lifecycle.
  public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate, onCreateView, and
    // onCreateView if the parent Activity is killed and restarted.

  // Called at the end of the visible lifetime.
  public void onStop(){
    // Suspend remaining UI updates, threads, or processing
    // that aren't required when the Fragment isn't visible.

  // Called when the Fragment's View has been detached.
  public void onDestroyView() {
    // Clean up resources related to the View.

  // Called at the end of the full lifetime.
  public void onDestroy(){
    // Clean up any resources including ending threads,
    // closing database connections etc.

  // Called when the Fragment has been detached from its parent Activity.
  public void onDetach() {