title: Destroy FragmentStatePagerAdapter instance with parent Fragment
body: <p>I am not sure how to ask this. I tried <a href="https://stackoverflow.com/questions/41412926/viewpager-fragment-loads-even-if-the-parent-fragment-is-not-active/">in here</a>, but I guess I was not clear enough. So, I thought I just write a small App to describe the situation. Please note, the App uses Googles <a href="https://www.google.com.au/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwjCsfjZ7Z_RAhWCKJQKHUznC9kQFggZMAA&amp;url=https%3A%2F%2Fdeveloper.android.com%2Fsamples%2FSlidingTabsBasic%2Fsrc%2Fcom.example.android.common%2Fview%2FSlidingTabLayout.html&amp;usg=AFQjCNE2Ra2Qi2iZSYuL7so1Vb871F-4UQ&amp;sig2=tFX-AQgNaVLM3Inyj6-Mzg&amp;bvm=bv.142059868,d.dGo" rel="nofollow noreferrer">SlidingTabLayout</a>.</p>
  
  <p>Long story short, at any point if I click Button1, the <code>FrameLayout</code> should contain <code>Fragment1</code>, removing <code>Fragment2</code> (if exists). Therefore, FragmentViewPager should also be destroyed as <code>Fragment2</code>. However, even then if I change the orientation of my device, I get the Toast which is defined in the <code>onCreate()</code> method of <code>FragmentViewPager</code>.</p>
  
  <p>Why <code>FragmentViewPager</code>'s onCreate is called even if <code>Fragment2</code> is paused/destroyed? Is it possible that, the Toast of <code>FragmentViewPager</code> will not be shown when <code>Fragment2</code> is destroyed?</p>
  
  <p><strong>MainActivity:</strong></p>
  
  <pre><code>package com.abdfahim.testproject;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button button1 = (Button) findViewById(R.id.button1);
          button1.setOnClickListener(onClickListener);
  
          Button button2 = (Button) findViewById(R.id.button2);
          button2.setOnClickListener(onClickListener);
      }
  
  
      private View.OnClickListener onClickListener = new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Fragment fragment;
              switch (v.getId()){
                  case R.id.button1:
                      fragment = new Fragment1();
                      break;
                  case R.id.button2:
                      fragment = new Fragment2();
                      break;
                  default:
                      return;
              }
              getFragmentManager().beginTransaction().replace(R.id.frame_container, fragment, v.getTag().toString()).addToBackStack(null).commit();
          }
      };
  }
  </code></pre>
  
  <p><strong>Fragment1</strong></p>
  
  <pre><code>package com.abdfahim.testproject;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class Fragment1 extends Fragment {
  
      public Fragment1(){}
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment1,container,false);
          setHasOptionsMenu(true);
  
          return rootView;
      }
  }
  </code></pre>
  
  <p><strong>Fragment2</strong></p>
  
  <pre><code>package com.abdfahim.testproject;
  
  import android.app.Fragment;
  import android.app.FragmentManager;
  import android.os.Bundle;
  import android.support.v13.app.FragmentStatePagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class Fragment2 extends Fragment {
  
      public Fragment2(){}
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment2,container,false);
          setHasOptionsMenu(true);
  
          CharSequence titles[]= {"Tab A", "Tab B"};
  
          // Creating The ViewPagerAdapter
          ViewPagerAdapter adapter =  new ViewPagerAdapter(getActivity().getFragmentManager(), titles, titles.length);
          ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
          pager.setAdapter(adapter);
  
          // Assigning the Sliding Tab Layout View
          SlidingTabLayout tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
  
          // Setting the ViewPager For the SlidingTabsLayout
          tabs.setViewPager(pager);
  
          return rootView;
      }
  
      static class ViewPagerAdapter extends FragmentStatePagerAdapter {
  
          private CharSequence titles[];
          private int numbOfTabs;
  
          public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabs) {
              super(fm);
  
              this.titles = mTitles;
              this.numbOfTabs = mNumbOfTabs;
  
          }
  
          @Override
          public int getItemPosition(Object object) {
              return POSITION_NONE;
          }
  
          @Override
          public Fragment getItem(int position) {
              Bundle bundle = new Bundle();
              bundle.putString("displayText", "Inside Fragment 2, " + titles[position]);
              FragmentViewPager fragment = new FragmentViewPager();
              fragment.setArguments(bundle);
              return fragment;
          }
  
          // This method return the titles for the Tabs in the Tab Strip
  
          @Override
          public CharSequence getPageTitle(int position) {
              return titles[position];
          }
  
          // This method return the Number of tabs for the tabs Strip
  
          @Override
          public int getCount() {
              return numbOfTabs;
          }
  
      }
  }
  </code></pre>
  
  <p><strong>FragmentViewPager</strong></p>
  
  <pre><code>package com.abdfahim.testproject;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class FragmentViewPager extends Fragment {
  
      public FragmentViewPager(){}
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_view_pager,container,false);
          setHasOptionsMenu(true);
  
          Bundle bundle = this.getArguments();
          TextView textView = (TextView) rootView.findViewById(R.id.tabText);
          textView.setText(bundle.getString("displayText"));
  
          return rootView;
      }
  
      @Override
      public void onStart() {
          super.onStart();
          Toast.makeText(getActivity(), "This is View Pager Fragment", Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>