private void addFragment(Fragment f, int index) {
    if (f == null) return;

    Log.e("ASDF", f + ": " + index);

    clearFragments(index + 1);

    int type = 0;
    boolean focused = false;

    PaneSizer paneSizer = mPaneSizer;
    if (paneSizer != null) {
      type = paneSizer.getType(f);
      focused = paneSizer.getFocused(f);
    }

    PaneView p = panesLayout.getPane(index);
    if (p != null && p.type == type) {
    } else {
      clearFragments(index);
      p = panesLayout.addPane(type, focused);
      if (p.index != index) throw new IllegalStateException("Added pane has wrong index");
    }

    panesLayout.setIndex(index);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(p.getInnerId(), f);
    ft.commit();

    fragmentStack.add(index, f);
    updateFragment(f);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.panes_layout);

    panesLayout = (PanesLayout) findViewById(R.id.panes);
    panesLayout.setOnIndexChangedListener(this);

    if (savedInstanceState != null) {
      int[] panesType = savedInstanceState.getIntArray("PanesLayout_panesType");
      boolean[] panesFocused = savedInstanceState.getBooleanArray("PanesLayout_panesFocused");
      int currentIndex = savedInstanceState.getInt("PanesLayout_currentIndex");
      for (int i = 0; i < panesType.length; i++) {
        panesLayout.addPane(panesType[i], panesFocused[i]);
      }
      panesLayout.setIndex(currentIndex);
    }

    if (savedInstanceState != null) {
      FragmentManager fm = getSupportFragmentManager();

      for (int index = 0; index < panesLayout.getNumPanes(); index++) {
        int id = panesLayout.getPane(index).getInnerId();
        Fragment f = fm.findFragmentById(id);

        fragmentStack.add(f);
        updateFragment(f);
      }
    }
  }
  /** Save the state of the panes */
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    int[] panesType = new int[panesLayout.getNumPanes()];
    boolean[] panesFocused = new boolean[panesLayout.getNumPanes()];
    for (int i = 0; i < panesLayout.getNumPanes(); i++) {
      PaneView p = panesLayout.getPane(i);
      panesType[i] = p.type;
      panesFocused[i] = p.focused;
    }

    savedInstanceState.putIntArray("PanesLayout_panesType", panesType);
    savedInstanceState.putBooleanArray("PanesLayout_panesFocused", panesFocused);
    savedInstanceState.putInt("PanesLayout_currentIndex", panesLayout.getCurrentIndex());
  }
 /** On action-bar home button selected, slide to the menu */
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case android.R.id.home:
       panesLayout.setIndex(0);
       return true;
   }
   return false;
 }
  @Override
  public void addFragment(Fragment oldFragment, Fragment newFragment) {
    if (newFragment != null) {
      int index = 1; // by default, add after menu

      int oldIndex = getIndex(oldFragment);
      if (oldIndex != -1) index = oldIndex + 1;

      addFragment(newFragment, index);
      updateFragment(newFragment);
    } else {
      panesLayout.setIndex(1);
    }
  }
  private void clearFragments(int index) {
    while (index < fragmentStack.size()) fragmentStack.remove(index);

    ArrayList<PaneView> panes = panesLayout.removePanes(index);
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    for (PaneView pane : panes) {
      int id = pane.getInnerId();
      Fragment f = fm.findFragmentById(id);
      if (f != null) ft.remove(f);
    }

    ft.commit();
  }
 /** Handle back press */
 @Override
 public boolean onBackPressed() {
   if (panesLayout.onBackPressed()) return true;
   return false;
 }
 @Override
 public void showMenu() {
   panesLayout.setIndex(0);
 }
 @Override
 public Fragment getTopFragment() {
   return getFragment(panesLayout.getNumPanes() - 1);
 }