title: Destroyed fragment recreated and redestroyed when restarting the app
body: <p>I'm trying do understand the lifecycle of <code>Fragment</code>.</p>
  
  <p>I have a <code>MainActivity</code>, with a layout including two <code>FrameLayout</code> to load <code>Fragments</code>, one for a top bar with a vertical menu, another one for the content.</p>
  
  <p>When I start the app, an instance of <code>HomeFragment</code> is loaded in the content <code>FrameLayout</code> container. Then when I click on a button in the menu, an instance of <code>SomeFragment</code> replaces it.</p>
  
  <p>When on the <code>SomeFragment</code> content, I click on the device's Home button, and all the visible <code>Fragment</code> (i.e. the <code>TopBarFragment</code> and the <code>SomeFragment</code>) are destroyed and detached from the <code>MainActivity</code>, i.e. <code>onDestroyView</code>, <code>onDestroy</code> and <code>onDetach</code> are called for both.</p>
  
  <p>Now when I restart the app, the <code>TopBarFragment</code> and the <code>SomeFragment</code> are created and destroyed (full lifecycle from <code>onAttach</code> to <code>onDetach</code>) and then the <code>TopBarFragment</code> and the <code>HomeFragment</code> are created, as expected from the code in the <code>MainActivity</code>'s <code>onCreate</code>.</p>
  
  <p>Why are the <code>TopBarFragment</code> and the <code>SomeFragment</code>, i.e. the latest visible <code>Fragment</code> before clicking on the device's Home button, recreated and destroyed before executing what's in the <code>MainActivity</code>'s <code>onCreate</code>?</p>
  
  <p>Note: in order to test my app I checked the option <code>Don't keep activities</code> in my device's developer options.</p>
  
  <p><b>MainActivity.java</b></p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);     
  
          setContentView(R.layout.main);      
  
          FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  
          Fragment homeFragment = new HomeFragment();
          fragmentTransaction.replace(R.id.content_container, homeFragment, "");
  
          Fragment topBarFragment = new TopBarFragment();
          fragmentTransaction.replace(R.id.top_bar_container, topBarFragment, "top_bar_fragment");
  
          fragmentTransaction.commit();
      }
  }
  </code></pre>
  
  <p><b>main.xml</b></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;FrameLayout
          android:id="@+id/content_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingTop="44dp" /&gt;
  
      &lt;FrameLayout
          android:id="@+id/top_bar_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:clipChildren="false" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><b>TopBarFragment.java</b></p>
  
  <pre><code>public class TopBarFragment extends Fragment{   
  
      private int mSelectedMenuOption = 0;
  
      private LinearLayout mVerticalMenu;
  
      private Boolean mMenuIsOpen = true;
  
      private ImageButton btn_01, btn_02; // there are more
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          return inflater.inflate(R.layout.top_bar, container, false);
      }
  
      @Override
      public void onActivityCreated (Bundle savedInstanceState){
  
          super.onActivityCreated(savedInstanceState);
  
          btn_01 = (ImageButton) getView().findViewById(R.id.btn_01);
          btn_02 = (ImageButton) getView().findViewById(R.id.btn_02);
  
          btn_01.setOnClickListener(mButtonClickListener);
          btn_02.setOnClickListener(mButtonClickListener);
  
          mVerticalMenu = (LinearLayout) getView().findViewById(R.id.vertical_menu);
  
          toggleMenu(0);
  
          Button btn_menu = (Button) getView().findViewById(R.id.btn_menu);
          btn_menu.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  // toggle vertical menu             
  
              }
          });
      }
  
  
      private OnClickListener mButtonClickListener = new OnClickListener()
      {
  
          @Override
          public void onClick(View v) {
  
              /* ... */
  
              if(!v.isSelected()){
  
                  FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
  
                  switch(v.getId()){
  
                  case R.id.btn_01:
  
                      Fragment homeFragment = new HomeFragment();     
                      fragmentTransaction.replace(R.id.content_container,homeFragment, "");
                      fragmentTransaction.commit();
  
                      break;
  
                  case R.id.btn_02:
  
                      Fragment someFragment = new SomeFragment();     
                      fragmentTransaction.replace(R.id.content_container, someFragment, "");
                      fragmentTransaction.commit();
  
                      break;      
                  }
              }
          }
      };    
  }
  </code></pre>
  
  <p><b>SomeFragment</b></p>
  
  <pre><code>public class SomeFragment extends Fragment{
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          ((TopBarFragment)getActivity().getSupportFragmentManager().findFragmentByTag("top_bar_fragment")).setSelectedButton(1);
  
          return inflater.inflate(R.layout.some_fragment, container, false);
      }
  }
  </code></pre>