title: NotifyDataSetChanged not working in FragmentStateAdapter with Viewpager2
body: <p>I am making a file selection page with viewpager2 with FragmentStateAdapter. In one of the pages I show all mounted storage devices on which I would like to tap to view the contents but I want the contents to be on another fragment.</p>
  <p>I want to replace fragment with another one in the last page of viewpager. but with the following code, the page just refreshes and new Fragment is not created as <code>createFragment</code> does not get called.</p>
  <p>My PagerAdapter</p>
  <pre><code>public static class PagerAdapter extends FragmentStateAdapter {
  
  
          private Fragment mFragmentAtPos0;
          private final FragmentManager mFragmentManager;
          private final FirstPageListener listener = new FirstPageListener();
          public final class FirstPageListener implements
                  FirstPageFragmentListener {
              public void onSwitchToNextFragment() {
                  mFragmentManager.beginTransaction().remove(mFragmentAtPos0)
                          .commit();
                  if (mFragmentAtPos0 instanceof FilesandFolder_Others_MainPage){
                      mFragmentAtPos0 = new FileExplorer(listener);
                  }else{ // Instance of NextFragment
                      mFragmentAtPos0 = new FilesandFolder_Others_MainPage(listener);
                  }
                  notifyDataSetChanged();
              }
              public void onSwitchToNextFragment(Bundle bundle) {
  //                mFragmentManager.beginTransaction().remove(mFragmentAtPos0)
  //                        .commit();
                  if (mFragmentAtPos0 instanceof FilesandFolder_Others_MainPage){
                      mFragmentAtPos0 = new FileExplorer(listener);
                      mFragmentAtPos0.setArguments(bundle);
                  }else{ // Instance of NextFragment
                      mFragmentAtPos0 = new FilesandFolder_Others_MainPage(listener);
                      mFragmentAtPos0.setArguments(bundle);
                  }
                  notifyDataSetChanged();
  //                notifyItemChanged(getItemPosition(mFragmentAtPos0));
              }
          }
  
  
  //        @Override
  //        public void onBindViewHolder(@NonNull FragmentViewHolder holder, int position, @NonNull List&lt;Object&gt; payloads) {
  //            super.onBindViewHolder(holder, position, payloads);
  //            if (position == getItemPosition(mFragmentAtPos0)) {
  //
  //                Fragment f = mFragmentManager.findFragmentByTag(&quot;f&quot; + holder.getItemId());
  //                if (f != null) {
  //                    mFragmentManager.beginTransaction().replace(holder.getItemId(), )
  //                }
  //            }
  //        }
  
          private int getItemPosition(Fragment mFragmentAtPos0) {
              for (int i = 0; i &lt; getItemCount(); i++){
  
                  if (createFragment(i).equals(mFragmentAtPos0)){
                      return i;
                  }
              }
              return -1;
          }
  
  
          public PagerAdapter(FragmentActivity fm) {
              super(fm);
              mFragmentManager = fm.getSupportFragmentManager();
              mFragmentAtPos0 = new FilesandFolder_Others_MainPage(listener);
          }
  
          @NonNull
          @Override
          public Fragment createFragment(int position) {
              switch (position){
                  case 0:
                      return new AppSelectionFragment();
                  case 1:
                      return new Photos();
                  case 2:
                      return new VideoGalleryFragment();
                  case 3:
                      return new Gallery();
                  default:
                      return mFragmentAtPos0;
              }
          }
  
          @Override
          public int getItemCount() {
              return 5;
          }
      }
  
  </code></pre>
  <p>Here is the constructor of Fragment from which I would like to replace to another Fragment:</p>
  <pre><code>    private static FileSelection.PagerAdapter.FirstPageListener pageFragmentListener;
  
      public FilesandFolder_Others_MainPage() {
      }
      public FilesandFolder_Others_MainPage(FileSelection.PagerAdapter.FirstPageListener firstPageFragmentListener) {
          pageFragmentListener = firstPageFragmentListener;
      }
  
  </code></pre>
  <p>And on tap of a view,
  the following code is supposed to be called:-</p>
  <pre><code>Bundle bundle = new Bundle();
  bundle.putString(&quot;PATH&quot;, volumes.get(position).path);
  pageFragmentListener.onSwitchToNextFragment(bundle);
  </code></pre>
  <p>I have searched all over stackoverflow, and never saw a proper answer with viewpager 2 and FragmentStateAdapter as everyone seems to be using Viewpager</p>
  <p>I tried to implement this:- <a href="https://stackoverflow.com/questions/60408236/fragmentstateadapter-for-viewpager2-notifyitemchanged-not-working-as-expected">FragmentStateAdapter for ViewPager2 notifyItemChanged not working as expected</a></p>
  <p>but couldn't understand how to edit my code. Pls help
  Thanks in advance</p>