@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);
      }
    }
  }
  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);
  }
  /** 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());
  }