title: Android oncreateview called twice
body: <p>I have see this question asked here more than one time but I can't figure it out how to solve for my case.</p>
  
  <p>I have an app that the user does this:</p>
  
  <p>1 - Open the navigationDrawer and selects one option (a fragment is created) (I'm here selecting the second option);</p>
  
  <pre><code>public void selectItem(int position) {
          Fragment fragment = null;
          switch (position) {
              case FRAGMENT_OPTION1:
                  ...
                  break;
              case FRAGMENT_OPTION2:
                  fragment = ControlPanelFragment.newInstance();
                  break;
              ...
              case FRAGMENT_OPTIONN:
                  ...
                  return;
              default:
                  break;
          }
          if (fragment != null) {
              FragmentManager fragmentManager = getSupportFragmentManager();
              fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commitAllowingStateLoss();
          }
  }
  </code></pre>
  
  <p>2 - The selected option (ControlPanelFragment) gets loaded:
      2.1 - Control panel has tabs and an iconpager. For each pager page and for each tab a new fragment is created. I have 3 tabs and 3 pages so 9 fragments are created;</p>
  
  <pre><code>@Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      if (savedInstanceState != null) {
          currentControlPanelOption = savedInstanceState.getInt("currentControlPanelOption", currentControlPanelOption);
          currentControlPanelTab = savedInstanceState.getInt("currentControlPanelTab", currentControlPanelTab);
      }
      setControlPanelTabs();
      setIconPager();
  }
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putInt("currentControlPanelOption", pager.getCurrentItem());
      outState.putInt("currentControlPanelTab", mTabHost.getCurrentTab());
  }
  </code></pre>
  
  <p>3 - In the <code>setIconPager();</code> I have this code:</p>
  
  <pre><code>pager = (ViewPager) view.findViewById(R.id.pager);
  cPanelPagerAdapter = new ControlPanelPagerAdapter(getChildFragmentManager());
  pager.setOffscreenPageLimit(2);
  pager.setAdapter(cPanelPagerAdapter);
  </code></pre>
  
  <p>where <code>ControlPanelPagerAdapter</code> has this code:</p>
  
  <pre><code>public Fragment getItem(int index) {
      Fragment fragment;
      switch (index) {
          case 1:
              fragment = FragmentA.newInstance();
              break;
          case 2:
              fragment = FragmentB.newInstance();
              break;
          case 3:
              fragment = FragmentC.newInstance();
              break;
          default:
              fragment = null;
              break;
      }
      ...
      return fragment;
  }
  </code></pre>
  
  <p>4 - <code>FragmentA</code>, <code>FragmentB</code> and <code>FragmentC</code> have almost the same code:</p>
  
  <pre><code>public static FragmentA newInstance() {
      return new FragmentA();
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_placeholder, container, false);
      return view;
  }
  
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      if (savedInstanceState == null) {
          fragmentA_Data = new FragmentADetail[3];
          createTabInstance(0);
      } else {
          fragmentA_Data = (FragmentADetail[]) savedInstanceState.getSerializable("Data");
          return;
      }
  }
  private void createTabInstance(int tab) {
      new FragmentADetail();
      fragment = FragmentADetail.newInstance(tab);
  
      Bundle args = new Bundle();
      args.putInt("tab", tab);
      fragment.setArguments(args);
  
      fragmentA_Data[tab] = fragment;
      FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.fragment_placeholder, fragmentA_Data[tab]);
      fragmentTransaction.commitAllowingStateLoss();
  }
  public void getTabData(int tab) {
      if (fragmentA_Data[tab] == null) {
          createStoreTimePeriodInstance(tab);
      } else {
          if (fragmentA_Data[tab].getArguments() == null) {
              Bundle args = new Bundle();
              args.putInt("tab", tab);
              fragmentA_Data[tab].setArguments(args);
          }
          FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
          fragmentTransaction.replace(R.id.fragment_placeholder, fragmentA_Data[tab]);
          fragmentTransaction.commitAllowingStateLoss();
      }
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putSerializable("data", fragmentA_Data);
  }
  </code></pre>
  
  <p>5 - Finally, <code>FragmentADetail</code> has this code:</p>
  
  <pre><code>public static FragmentADetail newInstance(int tab) {
      selectedTab = tab;
      return new FragmentADetail();
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      view = inflater.inflate(R.layout.details_fragment, container, false);
      ...
      if (savedInstanceState != null) {
          selectedTab = savedInstanceState.getInt("selectedTab");
      }
      ...
  }
  
  public void getTabData(int tab) {
      //This is where I'm getting the data that populates the layout
  }
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putInt("selectedTab", selectedTab);
  }
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      Bundle args = getArguments();
      if (args != null) {
         getTabData(args.getInt("tab"));
      }
  }
  </code></pre>
  
  <p>Now, imagine I'm on <code>FragmentA</code> with the third tab selected. If I rotate the screen I have this sequence of events:</p>
  
  <ol>
  <li>ControlPanelFragment <code>onSaveInstanceState</code> saves the current tab and current fragment</li>
  <li>FragmentA <code>onSaveInstanceState</code> saves the tabs fragments for the pager</li>
  <li>navigationDrawer second option gets again called <code>fragment = ControlPanelFragment.newInstance();</code></li>
  <li>ControlPanelFragment <code>onViewCreated</code> is called and I can get the saved data information and a new pager and tabs are created</li>
  <li>FragmentA <code>onViewCreated</code> is called and I can extract the saved data</li>
  <li>FragmentADetail <code>onActivityCreated</code> gets the saved data and loads the data correctelly (at least I think)
  And from now a second set of methods is called the second time and the data that was previously saved is reseted and so it now displays wrong data</li>
  <li>ControlPanelFragment <code>onSaveInstanceState</code> but now the savedInstanceState is null</li>
  <li>ControlPanelPagerAdapter <code>getItem</code> is called instantiating the 3 fragments</li>
  <li>FragmentA <code>onSaveInstanceState</code> its now called but savedInstanceState is null</li>
  <li>FragmentADetail <code>onActivityCreated</code> is called but now the tab = 0</li>
  </ol>
  
  <p>Can someone explain-me how can I stop steps 7 to 10 from happening?</p>