title: Lifecycle of hidden fragment
body: <p>I wanted to understand the interaction between activity lifecycle and fragment lifecycle in Android, so I decided to start a new project and copy code from <a href="http://www.html.it/pag/49111/layout-adattabili-smartphonetablet-in-android/" rel="nofollow">this page</a>.<br>
  I then thought that to improve my comprehension it would be useful to test it on a tablet, switch its orientation and see what it happens (although maybe it was not intended for this).
  Through logcat I kept track of the main events that are fired both on the activity and on the country fragment, for example in Activity Oncreate I write:</p>
  
  <pre><code>Log.d("TAG", "Activity: 1.onCreate");
  </code></pre>
  
  <p>This is what I get when I start from the portrait view and then rotate:</p>
  
  <blockquote>
    <p>TAG: Activity: 1.onCreate<br>
    TAG: Activity: 2.onStart<br>
    TAG: ## ContryFrag: 1.onAttach<br>
    TAG: ## ContryFrag: 2.onCreateView<br>
    TAG: ## ContryFrag: 3.onStart<br>
    TAG: Activity: 3.onResume<br>
    TAG: ## ContryFrag: 4.onResume<br>
    TAG: Activity: -3.onPause<br>
    TAG: ## ContryFrag: -4.onPause<br>
    TAG: Activity: -2.onStop<br>
    TAG: ## ContryFrag: -3.onStop<br>
    TAG: Activity: -1.onDestroy<br>
    TAG: ## ContryFrag: -2.onDestroyView<br>
    TAG: ## ContryFrag: -1.onDetach<br>
    TAG: Activity: 1.onCreate<br>
    TAG: ## ContryFrag: 1.onAttach<br>
    TAG: ## ContryFrag: 1.onAttach<br>
    TAG: ## ContryFrag: 2.onCreateView<br>
    TAG: Activity: 2.onStart<br>
    TAG: ## ContryFrag: 2.onCreateView<br>
    TAG: ## ContryFrag: 3.onStart<br>
    TAG: ## ContryFrag: 3.onStart<br>
    TAG: Activity: 3.onResume<br>
    TAG: ## ContryFrag: 4.onResume<br>
    TAG: ## ContryFrag: 4.onResume </p>
  </blockquote>
  
  <p>It seems that the fragment that was added via the FragmentManager in the portrait view is still recreated after rotation, despite it was detached before. The event chain to resume it is followed again, even if it won’t be displayed.<br>
  Can you help me understand what is happening here?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>When I check if the fragment isInLayout()  </p>
  
  <pre><code>Log.d("TAG", "## ContryFrag: 1.onAttach /" + isInLayout() );
  </code></pre>
  
  <p>I realize that there are two fragments after rotation: one created with the rotation and a second one who seem to be a remainder of the previous activity (which shouldn't be).</p>
  
  <blockquote>
    <p>TAG: Activity: 1.onCreate<br>
    TAG: ## ContryFrag: 1.onAttach /false<br>
    TAG: ## ContryFrag: 1.onAttach /true<br>
    TAG: ## ContryFrag: 2.onCreateView /true<br>
    TAG: Activity: 2.onStart<br>
    TAG: ## ContryFrag: 2.onCreateView /false<br>
    TAG: ## ContryFrag: 3.onStart /false<br>
    TAG: ## ContryFrag: 3.onStart /true<br>
    TAG: Activity: 3.onResume<br>
    TAG: ## ContryFrag: 4.onResume /false<br>
    TAG: ## ContryFrag: 4.onResume /true   </p>
  </blockquote>
  
  <p><strong>EDIT 2</strong>   </p>
  
  <p>This is the dummy code I used to get onSelectedCountry(String country), which is not provided:</p>
  
  <pre><code>public void onSelectedCountry(String country)
  {
      String[] x = {country, "city0", "city1", "city2", "city3", "city4", "city5"};
      adapter.clear();
      adapter.addAll(x);
  }
  </code></pre>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>Here is <a href="https://github.com/giusdin/androidfragment" rel="nofollow">my code</a>. Sorry for the delay, it was the first time I used github.</p>