title: Fragment gets initialized twice when reloading activity with tabs when orientation changes
body: <p>I have a problem reloading an activity with tabs and fragments when I change the orientation of my device.</p>
  
  <p>Here's the situation:</p>
  
  <p>I have an activity which has 3 tabs in the action bar. Each tab loads a different fragment in a <code>FrameLayout</code> in main view. Everything works fine if I don't change the orientation of the device. But when I do that Android tries to initialize the currently selected fragment twice which produce the following error:</p>
  
  <pre><code>E/AndroidRuntime(2022): Caused by: android.view.InflateException: Binary XML file line #39: Error inflating class fragment
  </code></pre>
  
  <p>Here's the sequence of steps that produce the error:</p>
  
  <ol>
  <li>I load the activity, select tab nr 2. and change the orientation of the device.</li>
  <li>Android destroys the activity and the instance of the fragment loaded by tab nr 2 (from now on, 'Fragment 2'). Then it proceeds to create new instances of the activity and the fragment.</li>
  <li>Inside <code>Activity.onCreate()</code> I add the first tab to the action bar. When I do that, this tab gets automatically selected. It may represent a problem in the future, but I don't mind about that now. <code>onTabSelected</code> gets called and a new instance of the first fragment is created and loaded (see code below).</li>
  <li>I add all the other tabs without any event being triggered, which is fine.</li>
  <li>I call <code>ActionBar.selectTab(myTab)</code> to select Tab nr 2.</li>
  <li><code>onTabUnselected()</code> gets called for the first tab, and then <code>onTabSelected()</code> for the second tab. This sequence replaces the current fragment for an instance of Fragment 2 (see code below).</li>
  <li>Next, <code>Fragment.onCreateView()</code> is called on Fragment 2 instance and the fragment layout gets inflated.</li>
  <li>Here is the problem. Android Calls <code>onCreate()</code> and then <code>onCreateView()</code> on the fragment instance ONCE AGAIN, which produces the exception when I try to inflate (a second time) the layout.</li>
  </ol>
  
  <p>Obviously the problem is Android is initializing the fragment twice, but I don't know why.</p>
  
  <p>I tried NOT selecting the second tab when I reaload the activity but the second fragment gets initialized anyway and it is not shown (since I didn't select its tab).</p>
  
  <p>I found this question: <a href="https://stackoverflow.com/q/10634854/48468">Android Fragments recreated on orientation change</a></p>
  
  <p>The user asks basically the same I do, but I don't like the chosen answer (it's only a workaroud). There must be some way to get this working without the <code>android:configChanges</code> trick.</p>
  
  <p>In case it's not clear, what I want to know how whether to prevent the recreation of the fragment or to avoid the double initialization of it. It would be nice to know why is this happening also. :P</p>
  
  <p>Here is the relevant code:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MyActivity extends Activity  implements ActionBar.TabListener {
  
      private static final String TAG_FRAGMENT_1 = "frag1";
      private static final String TAG_FRAGMENT_2 = "frag2";
      private static final String TAG_FRAGMENT_3 = "frag3";
  
      Fragment frag1;
      Fragment frag2;
      Fragment frag3;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // my_layout contains a FragmentLayout inside
          setContentView(R.layout.my_layout); 
  
          // Get a reference to the fragments created automatically by Android
          // when reloading the activity
          FragmentManager fm = getFragmentManager();
          this.frag1 = fm.findFragmentByTag(MyActivity.TAG_FRAGMENT_1);
          this.frag2 = fm.findFragmentByTag(MyActivity.TAG_FRAGMENT_2);
          this.frag3 = fm.findFragmentByTag(MyActivity.TAG_FRAGMENT_3)
  
  
          ActionBar actionBar = getActionBar();
  
          // snip...
  
          // This triggers onTabSelected for the first tab
          actionBar.addTab(actionBar.newTab()
                  .setText("Tab1").setTabListener(this)
                  .setTag(MyActivity.TAG_FRAGMENT_1));
  
          actionBar.addTab(actionBar.newTab()
                  .setText("Tab2").setTabListener(this)
                  .setTag(MyActivity.TAG_FRAGMENT_2));
          actionBar.addTab(actionBar.newTab()
                  .setText("Tab3").setTabListener(this)
                  .setTag(MyActivity.TAG_FRAGMENT_3));
  
          Tab t = null;
          // here I get a reference to the tab that must be selected
          // snip...
  
          // This triggers onTabUnselected/onTabSelected
          ab.selectTab(t);
  
      }
  
      @Override
      protected void onDestroy() {
          // Not sure if this is necessary
          this.frag1 = null;
          this.frag2 = null;
          this.frag3 = null;
          super.onDestroy();
      }
  
      @Override  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {  
  
          Fragment curFrag = getFragmentInstanceForTag(tab.getTag().toString());
          if (curFrag == null) {
              curFrag = createFragmentInstanceForTag(tab.getTag().toString());
              if(curFrag == null) { 
                  // snip... 
                  return;
              }
          }
          ft.replace(R.id.fragment_container, curFrag, tab.getTag().toString());
      }
  
      @Override  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) 
      {  
          Fragment curFrag = getFragmentInstanceForTag(tab.getTag().toString());
          if (curFrag == null) {
              // snip... 
              return;
          }
  
          ft.remove(curFrag);
      }
  
      private Fragment getFragmentInstanceForTag(String tag) 
      {
          // Returns this.frag1, this.frag2 or this.frag3
          // depending on which tag was passed as parameter
      }
  
      private Fragment createFragmentInstanceForTag(String tag) 
      {
          // Returns a new instance of the fragment requested by tag
          // and assigns it to this.frag1, this.frag2 or this.frag3
      }
  }
  </code></pre>
  
  <p>The code for the Fragment is irrelevant, it just returns an inflated view on <code>onCreateView()</code> method override.</p>