title: fragment's onCreateView called multiple times
body: <p>So I have a Settings fragment whose <code>onCreateView</code> gets called multiple times. I have ViewPager which is inside of the fragment's layout and the fragment launches by a clicking on the settings icon from options menu in toolbar.</p>
  <p>And I cannot just move the ViewPager to the activity because I want to be able to swipe back to the Previous Fragment.</p>
  <p>Also,I tried removing the fragments in the backstack using</p>
  <pre class="lang-java prettyprint-override"><code>for(int i=0;i&lt; Objects.requireNonNull(fm).getBackStackEntryCount();++i){
        fm.popBackStackImmediate();
     }
  </code></pre>
  <p>but that did not help at all.</p>
  <p>Fragment code:</p>
  <pre class="lang-java prettyprint-override"><code>public class SettingsFragment extends Fragment {
      
      public SettingsFragment() {
     
      }
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
      }
      @Deprecated
      @Override
      public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                               Bundle savedInstanceState) {
          final View rootview = inflater.inflate(R.layout.fragment_settings, container, false);
          final CustomViewPager viewPager=rootview.findViewById(R.id.SettingsFragmentViewPager);
          viewPager.setAdapter(new Adapter(getFragmentManager()));
          viewPager.setOffscreenPageLimit(2);
          viewPager.setCurrentItem(1);
          viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
  
              @Override
              public void onPageSelected(int position) {
                  if (position == 0) {
                      viewPager.setPagingEnabled(false);
                      FragmentManager fm=getFragmentManager();
                      for(int i=0;i&lt; Objects.requireNonNull(fm).getBackStackEntryCount();++i){
                          fm.popBackStackImmediate();
                      }
                      viewPager.removeOnPageChangeListener(this);
                  }
              }
  
  
          });
          return rootview;
         
      }
  </code></pre>
  <p>Fragment's xml:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  
                                                     xmlns:tools=&quot;http://schemas.android.com/tools&quot;
                                                     xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
                                                     android:layout_height=&quot;match_parent&quot;
                                                     android:layout_width=&quot;match_parent&quot;
                                                     android:background=&quot;#fff&quot; android:animationCache=&quot;true&quot;
                                                     android:id=&quot;@+id/SettingsFragmentContainer&quot;
                                                     android:clickable=&quot;true&quot;
                                                     android:drawingCacheQuality=&quot;high&quot; android:focusable=&quot;true&quot;&gt;
  
  
          &lt;androidx.appcompat.widget.Toolbar
                  android:layout_width=&quot;0dp&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:background=&quot;?attr/colorPrimary&quot;
                  android:theme=&quot;?attr/actionBarTheme&quot;
                  android:minHeight=&quot;?attr/actionBarSize&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  android:id=&quot;@+id/SettingsActivityToolbar&quot; app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                  app:layout_constraintHorizontal_bias=&quot;1.0&quot;/&gt;
          &lt;TextView
                  android:text=&quot;Settings&quot;
                  android:layout_width=&quot;65dp&quot;
                  android:layout_height=&quot;23dp&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot; android:layout_marginTop=&quot;18dp&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:id=&quot;@+id/Settings_textview&quot;
                  android:textAppearance=&quot;@style/ActionBar.nameText&quot;/&gt;
          &lt;ImageButton
                  android:layout_width=&quot;46dp&quot;
                  android:layout_height=&quot;34dp&quot; app:srcCompat=&quot;@drawable/settings_icon_back&quot;
                  android:id=&quot;@+id/backPressButton&quot;
                  android:rotation=&quot;180&quot; android:backgroundTint=&quot;#fff&quot;
                  android:background=&quot;@null&quot;
                  app:layout_constraintStart_toStartOf=&quot;@+id/SettingsActivityToolbar&quot;
                  android:layout_marginStart=&quot;2dp&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                  android:layout_marginTop=&quot;16dp&quot; android:onClick=&quot;click&quot; android:drawingCacheQuality=&quot;high&quot;/&gt;
          &lt;TextView
                  android:text=&quot;&quot;
                  android:layout_width=&quot;0dp&quot;
                  android:layout_height=&quot;0.1dp&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:id=&quot;@+id/linebreak1&quot; android:layout_marginTop=&quot;10dp&quot;
                  app:layout_constraintTop_toBottomOf=&quot;@+id/backPressButton&quot; android:background=&quot;#A1A8AC&quot;/&gt;
          &lt;TextView
                  android:layout_width=&quot;0dp&quot;
                  android:layout_height=&quot;0.1dp&quot; android:id=&quot;@+id/linebreak2&quot;
                  android:background=&quot;#A1A8AC&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:layout_marginTop=&quot;7dp&quot;
                  app:layout_constraintTop_toBottomOf=&quot;@+id/switch4&quot;/&gt;
          &lt;Switch
                  android:text=&quot;Test Test&quot;
                  android:layout_width=&quot;0dp&quot;
                  android:layout_height=&quot;47dp&quot; android:id=&quot;@+id/switch4&quot;
                  android:textAppearance=&quot;@style/ActionBar.nameText&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  android:layout_marginStart=&quot;25dp&quot; android:layout_marginEnd=&quot;25dp&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot; android:thumbTint=&quot;@drawable/switch_thumb_selector&quot;
                  android:trackTint=&quot;@drawable/switch_track_selector&quot; android:layout_marginTop=&quot;18dp&quot;
                  app:layout_constraintTop_toBottomOf=&quot;@+id/backPressButton&quot; tools:ignore=&quot;UseSwitchCompatOrMaterialXml&quot;/&gt;
          &lt;TextView
                  android:layout_width=&quot;0dp&quot;
                  android:layout_height=&quot;67dp&quot; android:id=&quot;@+id/update&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                  android:clickable=&quot;true&quot;
                  android:textAppearance=&quot;@style/ActionBar.nameText&quot; app:layout_constraintHorizontal_bias=&quot;0.0&quot;
                  android:background=&quot;?attr/selectableItemBackground&quot; android:layout_marginTop=&quot;16dp&quot;
                  app:layout_constraintTop_toBottomOf=&quot;@+id/switch4&quot; android:onClick=&quot;onClickUpdate&quot;
                  android:focusable=&quot;true&quot;/&gt;
          &lt;TextView
                  android:text=&quot;test test&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:clickable=&quot;true&quot;
                  android:layout_height=&quot;0dp&quot; android:id=&quot;@+id/enter_version&quot;
                  app:layout_constraintTop_toTopOf=&quot;@+id/update&quot;
                  android:layout_marginTop=&quot;36dp&quot; android:layout_marginEnd=&quot;206dp&quot;
                  app:layout_constraintEnd_toEndOf=&quot;@+id/update&quot; android:focusable=&quot;true&quot;/&gt;
          &lt;TextView
                  android:text=&quot;Check Update&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:clickable=&quot;true&quot;
                  android:id=&quot;@+id/update_textview&quot;
                  android:textAppearance=&quot;@style/ActionBar.nameText&quot; android:layout_marginEnd=&quot;228dp&quot;
                  app:layout_constraintEnd_toEndOf=&quot;@+id/update&quot; app:layout_constraintTop_toTopOf=&quot;@+id/update&quot;
                  android:layout_marginTop=&quot;10dp&quot; android:focusable=&quot;true&quot;/&gt;
          &lt;com.testapp.test2.CustomViewPager
                  android:layout_width=&quot;0dp&quot;
                  android:layout_height=&quot;0dp&quot; app:layout_constraintTop_toTopOf=&quot;parent&quot;
                  app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot; app:layout_constraintVertical_bias=&quot;0.58000004&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot; android:id=&quot;@+id/SettingsFragmentViewPager&quot;
                  /&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>The adapter I am using for ViewPager:</p>
  <pre class="lang-java prettyprint-override"><code>public class Adapter extends FragmentStatePagerAdapter {
  
      public Adapter(FragmentManager fragmentManager){
          super(fragmentManager,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
  
      }
  
  
      @NotNull
      @Override
      public Fragment getItem(int position) {
          if(position==0){
              return new BlankFragment();
          }
  
          if(position==1){
              return new SettingsFragment();
          }
  
  
          return null;
          }
  
  
  
  
      @Override
      public int getCount() {
          return 2;
      }
  
  
  }
  </code></pre>
  <p>I see that in my adapter I am also returning <code>SettingsFragment</code>.Is there a way to return the fragment that was created when I clicked the settings button in the toolbar?</p>
  <p>Btw, I add the fragment using this:</p>
  <pre class="lang-java prettyprint-override"><code>    @Override
      public boolean onOptionsItemSelected(MenuItem item){
          switch(item.getItemId()){
              case R.id.updateId:
                  Log.d(&quot;MAIN ACTIVITY---&quot;,&quot;ADDING FRAGMENT...&quot;);
                  FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
                  ft.setCustomAnimations(R.anim.push_right_in,0);
                  ft.add(R.id.MainActivityConstraint,new SettingsFragment());
                  ft.commit();
                  Log.d(&quot;ACTIVITY---&quot;,&quot;Fragment added...&quot;);
                  break;
  
                 }
  }
  </code></pre>