title: Android childfragments from viewpager getActivity returns null
body: <p>I have an activity and with a button I am switching between the two fragments (MAIN &amp; SETTINGS). In the MAIN fragment I have a <code>ViewPager</code> with 4 child fragments.</p>
  <p>At first run everything works fine, but if I rotate the screen, the <code>getActivity()</code> for fragments within the <code>ViewPager</code> is returning null.</p>
  <p><strong>ActivityMain</strong>:</p>
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // Add or show the fragments
      showHideScreenFragment(FRAGMENT_MAIN);
  }
  
  private void showHideScreenFragment(String tag) {   
  
      FragmentManager fm = getSupportFragmentManager();
      FragmentTransaction ft = fm.beginTransaction();
      
      ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
      
      // Get the fragment from the backstack if it is existing
      BaseFragment oldFragment = getFragmentFromBackstack(tag);
  
      // Get the current fragment from the layout
      BaseFragment currentFragment = getCurrentFragment();
      
      if (oldFragment == null) {
          if (currentFragment != null) {
               ft.hide(currentFragment);
          }
          
          ft.add(getMainContainerId(), getFragmentInstance(tag), tag);
      }
      else {
  
          if (currentFragment != null) {
              if (isSameFragment(oldFragment, currentFragment))
                  return;
              
              ft.hide(currentFragment);
          }
          
          if (oldFragment.isHidden())
              ft.show(oldFragment);
      }
          
      ft.commit();
      fm.executePendingTransactions();
  }
  
  private BaseFragment getFragmentInstance(String tag) {
      if (tag.equals(FRAGMENT_MAIN)) return getFragmentMain();
      if (tag.equals(FRAGMENT_SETTINGS)) return getFragmentSettings();
      
      throw new RuntimeException(&quot;Fragment not found !&quot;);
  }
  
  private FragmentMain getFragmentMain() {
      return new FragmentMain();
  }
  
  private FragmentSettings getFragmentSettings() {
      return new FragmentSettings();
  }
  
  private BaseFragment getFragmentFromBackstack(String tag) {
      if (tag.equals(FRAGMENT_MAIN)) return getFragmentMainFromBackstack();
      if (tag.equals(FRAGMENT_SETTINGS)) return getFragmentSettingsFromBackstack();
      
      throw new RuntimeException(&quot;Fragment not found !&quot;);
  }
  
  private FragmentMain getFragmentMainFromBackstack() {
      return (FragmentMain) getSupportFragmentManager().findFragmentByTag(FRAGMENT_MAIN);
  }
  
  private FragmentSettings getFragmentSettingsFromBackstack() {
      return (FragmentSettings) getSupportFragmentManager().findFragmentByTag(FRAGMENT_SETTINGS);
  }
  
  private boolean isSameFragment(Fragment f1, Fragment f2) {
      return f1.getTag().equals(f2.getTag());
  }
  </code></pre>
  <p><strong>FragmentMain</strong>:</p>
  <pre><code>    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         
          View view = inflater.inflate(R.layout.fragment_main, container, false);
          
          viewPager = (ViewPager) view.findViewById(R.id.viewPager);
          
          // Add the 4 child fragments to the viewpager
          populateViewPager();
          
          // Debugging
          new Handler().postDelayed(new Runnable() {
              @Override
              public void run() {
                  _printFragmentStates();
              }
          }, 2500);
          
          return view;
      }
  
      private void populateViewPager() {      
          ArrayList&lt;BaseMainFragment&gt; fragments = new ArrayList&lt;BaseMainFragment&gt;();
          
          fragments.add(new FragmentSearch());
          fragments.add(new FragmentFavorites());
          fragments.add(new FragmentHouse());
          fragments.add(new FragmentRoom());
          
          adapterMain = new AdapterMain(getChildFragmentManager(), fragments);
          
          viewPager.setOffscreenPageLimit(4);
          viewPager.setAdapter(adapterMain);
      }
  
      // DEBUGGING
      private void _printFragmentStates() {
          Activity actSearch = null;
          Activity actFav = null;
          Activity actHouse = null;
          Activity actRoom = null;
      
          actSearch = getFragmentSearch().getActivity();
          actFav = getFragmentFavorites().getActivity();
          actHouse = getFragmentHouse().getActivity();
          actRoom = getFragmentRoom().getActivity();
      
          Functions.logd(&quot;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&quot;);
          Functions.logd(&quot;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&quot;);
          Functions.logd(&quot;Main fragment act, is null: &quot; + (getActivity() == null));
          Functions.logd(&quot;Search act, is null: &quot; + (actSearch == null));
          Functions.logd(&quot;Favorite act, is null: &quot; + (actFav == null));
          Functions.logd(&quot;House act, is null: &quot; + (actHouse == null));
          Functions.logd(&quot;Room act, is null: &quot; + (actRoom == null));
          Functions.logd(&quot;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&quot;);
          Functions.logd(&quot;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&quot;);
  }
  
  private FragmentSearch getFragmentSearch() {
      return (FragmentSearch) adapterMain.getItem(0);
  }
  
  private FragmentFavorite getFragmentFavorite() {
      return (FragmentFavorite) adapterMain.getItem(1);
  }
  
  private FragmentHouse getFragmentHouse() {
      return (FragmentHouse) adapterMain.getItem(2);
  }
  
  private FragmentRoom getFragmentHouse() {
      return (FragmentRoom) adapterMain.getItem(3);
  }
  </code></pre>
  <p>As I said, at first run everything works fine, but after I rotate the screen, I am getting null for <code>getActivity();</code> in the 4 child fragments: Search, Favorite, House and Room.</p>
  <h1>Logcat debug</h1>
  <p><strong>1 run:</strong></p>
  <pre><code>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Main fragment act, is null: false
  Search act, is null: false
  Favorite act, is null: false
  House act, is null: false
  Room act, is null: false
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  <p><strong>After screen orientation changed:</strong></p>
  <pre><code>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Main fragment act, is null: false
  Search act, is null: true
  Favorite act, is null: true
  House act, is null: true
  Room act, is null: true
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  <p>What am I doing wrong?</p>