Android Fragments

This video, part of my Android Quick Code Access series of posts, explains Android Fragments.  It's from my course Learning Android App Programming published by InfiniteSkills.

The Java source code for the example with additional comments for clarification...

// Copyright (C) 2010 The Android Open Source Project

package com.example.android.apis.app;

import com.example.android.apis.R;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

// Demonstration of hiding and showing fragments.
public class FragmentHideShow extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the display layout.
        setContentView(R.layout.fragment_hide_show);

        // Instantiate a Fragment Manager.
        FragmentManager fm = getFragmentManager();

        // Attached listeners to the fragments.
        addShowHideListener(R.id.frag1hide,
                    fm.findFragmentById(R.id.fragment1));
        addShowHideListener(R.id.frag2hide,
                    fm.findFragmentById(R.id.fragment2));
    }
    // Show and Hide Listener method.
    void addShowHideListener(int buttonId, 
                             final Fragment fragment) {
        // Button variable.
        final Button button = (Button)findViewById(buttonId);

        // Attach the onClickListener to the Button.
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                // Get and start a Fragment Manager.
                FragmentTransaction ft =
                    getFragmentManager().beginTransaction();

                // Set a fade in and out custom animation.
                ft.setCustomAnimations(
                    android.R.animator.fade_in,
                    android.R.animator.fade_out);

                // Perform the fragment show and hide.
                if (fragment.isHidden()) {
                    ft.show(fragment);
                    button.setText("Hide");
                } else {
                    ft.hide(fragment);
                    button.setText("Show");
                }
                // Commit the fragment transaction.
                ft.commit();
            }
        });
    }
    // First Fragment Class.
    public static class FirstFragment extends Fragment {

        // TextView for the changeable text.
        TextView mTextView;

        // Method invoked when the view is created.
        @Override
        public View onCreateView(
                        LayoutInflater inflater,
                        ViewGroup container,
                        Bundle savedInstanceState) {

            // Inflate the view.
            View v = inflater.inflate(
                            R.layout.labeled_text_edit, 
                            container, false);

            // Get the id of the message.
            View tv = v.findViewById(R.id.msg);

            // Display the text.
            ((TextView)tv)
                .setText("The fragment saves and restores this text.");

            // Retrieve the text editor.
            mTextView = (TextView)v.findViewById(R.id.saved);

            // Restore the last saved state if needed.
            if (savedInstanceState != null) {
                mTextView.setText(
                   savedInstanceState.getCharSequence("text"));
            }
            // Return the view.
            return v;
        }
        // Method defining instance state saving action.
        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);

            // Save the current text. 
            outState.putCharSequence("text", mTextView.getText());
        }
    }
    // Second Fragment Class.
    public static class SecondFragment extends Fragment {

        // Method invoked when the view is created.
        @Override
        public View onCreateView(
                        LayoutInflater inflater,
                        ViewGroup container,
                        Bundle savedInstanceState) {

            // Inflate the view.
            View v = inflater.inflate(
                        R.layout.labeled_text_edit, 
                        container, false);

            // Get the id of the message.
            View tv = v.findViewById(R.id.msg);

            // Display the text.
            ((TextView)tv).setText(
                 "The TextView saves and restores this text.");

            // Retrieve the text editor and 
            // tell it to save and restore its state.
            // Note that you will often set this 
            // in the layout XML, but since
            // we are sharing our layout with the other
            // fragment we will customize
            // it here.
            ((TextView)v.findViewById(R.id.saved))
                .setSaveEnabled(true);

            // Return the view.
            return v;
        }
    }
}