/** Create a new instance of CountingFragment, providing "num" as an argument. */
    static CountingFragment newInstance(int num) {
      CountingFragment f = new CountingFragment();

      // Supply num input as an argument.
      Bundle args = new Bundle();
      args.putInt("num", num);
      f.setArguments(args);

      return f;
    }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_stack);

    // Watch for button clicks.
    Button button = (Button) findViewById(R.id.new_fragment);
    button.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            addFragmentToStack();
          }
        });
    button = (Button) findViewById(R.id.home);
    button.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            // If there is a back stack, pop it all.
            FragmentManager fm = getSupportFragmentManager();
            if (fm.getBackStackEntryCount() > 0) {
              fm.popBackStack(
                  fm.getBackStackEntryAt(0).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
          }
        });

    if (savedInstanceState == null) {
      // Do first time initialization -- add initial fragment.
      Fragment newFragment = CountingFragment.newInstance(mStackLevel);
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      ft.add(R.id.simple_fragment, newFragment).commit();
    } else {
      mStackLevel = savedInstanceState.getInt("level");
    }
  }
  void addFragmentToStack() {
    mStackLevel++;

    // Instantiate a new fragment.
    Fragment newFragment = CountingFragment.newInstance(mStackLevel);

    // Add the fragment to the activity, pushing this transaction
    // on to the back stack.
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.simple_fragment, newFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
  }
  // BEGIN_INCLUDE(add_stack)
  void addFragmentToStack() {
    mStackLevel++;

    // Instantiate a new fragment.
    Fragment newFragment = CountingFragment.newInstance(mStackLevel);

    // Add the fragment to the activity, pushing this transaction
    // on to the back stack.
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(
        R.anim.fragment_slide_left_enter,
        R.anim.fragment_slide_left_exit,
        R.anim.fragment_slide_right_enter,
        R.anim.fragment_slide_right_exit);
    ft.replace(R.id.simple_fragment, newFragment);
    ft.addToBackStack(null);
    ft.commit();
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_stack);

    // Watch for button clicks.
    Button button = (Button) findViewById(R.id.new_fragment);
    button.setOnClickListener(
        new OnClickListener() {
          public void onClick(View v) {
            addFragmentToStack();
          }
        });

    if (savedInstanceState == null) {
      // Do first time initialization -- add initial fragment.
      Fragment newFragment = CountingFragment.newInstance(mStackLevel);
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      ft.add(R.id.simple_fragment, newFragment).commit();
    } else {
      mStackLevel = savedInstanceState.getInt("level");
    }
  }