title: How to get the current fragment displayed in a specific tab of a viewpager?
body: <p>I want to get the last fragment in the backstack, or the current displayed it's the same for me, in the <code>tab b_1</code>. As you can see in the following image, I have a ViewPager, and another one inner <code>tab b</code>. Thus there are four current fragments displayed.</p>
  
  <p><strong>Question</strong>: How can I get the <code>Fragment 2</code> instance?</p>
  
  <p>I have seen another solutions, but none works for this scenario.</p>
  
  <p><strong>Annotation:</strong> The fragment to return is not necessary the hosted in the ViewPager. I can have opened two more fragments in a tab.</p>
  
  <p><a href="https://i.stack.imgur.com/S1aP1.png" rel="noreferrer"><img src="https://i.stack.imgur.com/S1aP1.png" alt="scenario"></a></p>
  
  <p>With this method I get all the current visible fragments, but not the one specific I want.</p>
  
  <pre><code>public ArrayList&lt;Fragment&gt; getVisibleFragment() {
      List&lt;Fragment&gt; fragments = getSupportFragmentManager().getFragments();
      ArrayList&lt;Fragment&gt; visibleFragments = new ArrayList&lt;&gt;();
      if (fragments != null) {
          for (Fragment fragment : fragments) {
              if (fragment != null &amp;&amp; fragment.isVisible())
                  visibleFragments.add(fragment);
          }
      }
      return visibleFragments;
  }
  </code></pre>
  
  <p><strong>Some interesting code</strong></p>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;android.support.design.widget.CoordinatorLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"&gt;
  
          &lt;android.support.design.widget.TabLayout
              android:id="@+id/tabs"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:tabMode="fixed"
              app:tabGravity="fill"/&gt;
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/viewpager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"/&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private static ViewPagerAdapter adapter;
      private static ViewPager viewPager;
      private TabLayout tabLayout;
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          viewPager = (ViewPager) findViewById(R.id.viewpager);
          setupViewPager();
  
          tabLayout = (TabLayout) findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(viewPager);
          setupTabIcons();
      }
  
      private void setupViewPager() {
          adapter = new ViewPagerAdapter(getSupportFragmentManager());
          // Wrap with HostFragment to get separate tabbed nagivation.
          adapter.addFrag(HostFragment.newInstance(new Fragment1()), null);
          adapter.addFrag(HostFragment.newInstance(new RootFragment2()), null);
          adapter.addFrag(HostFragment.newInstance(new Fragment4()), null);
          viewPager.setAdapter(adapter);
          viewPager.setOffscreenPageLimit(2);
      }
  
      public void openNewFragment(Fragment fragment) {
          HostFragment hostFragment = (HostFragment) adapter.getItem(viewPager.getCurrentItem());
          hostFragment.replaceFragment(fragment, true);
      }
  }
  </code></pre>
  
  <p>fragment_host.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/hosted_fragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/&gt;
  </code></pre>
  
  <p>HostFragment.java</p>
  
  <pre><code>/**
   * This class implements separate navigation for a tabbed viewpager.
   *
   * Based on https://medium.com/@nilan/separate-back-navigation-for-
   * a-tabbed-view-pager-in-android-459859f607e4#.u96of4m4x
   */
  public class HostFragment extends BackStackFragment {
  
      private Fragment fragment;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          super.onCreateView(inflater, container, savedInstanceState);
          View view = inflater.inflate(R.layout.fragment_host, container, false);
          if (fragment != null) {
              replaceFragment(fragment, false);
          }
          return view;
      }
  
      public void replaceFragment(Fragment fragment, boolean addToBackstack) {
          if (addToBackstack) {
              getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).addToBackStack(null).commit();
          } else {
              getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).commit();
          }
      }
  
      public static HostFragment newInstance(Fragment fragment) {
          HostFragment hostFragment = new HostFragment();
          hostFragment.fragment = fragment;
          return hostFragment;
      }
  
      public Fragment getFragment() {
          return fragment;
      }
  }
  </code></pre>
  
  <p>fragment2_root.xml</p>
  
  <pre><code>&lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/fragment2_root"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;android.support.design.widget.TabLayout
          android:id="@+id/tab2_tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabMode="fixed"
          app:tabGravity="fill"/&gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/tab2_viewpager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>RootFragment2.java</p>
  
  <pre><code>public class RootFragment2 extends Fragment {
  
      private ViewPagerAdapter adapter;
      private ViewPager viewPager;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // Inflate the layout for this fragment.
          View root = inflater.inflate(R.layout.fragment2_root, container, false);
  
          viewPager = (ViewPager) root.findViewById(R.id.tab2_viewpager);
          setupViewPager(viewPager);
  
          TabLayout tabLayout = (TabLayout) root.findViewById(R.id.tab2_tabs);
          tabLayout.setupWithViewPager(viewPager);
  
          return root;
      }
  
      private void setupViewPager(ViewPager viewPager) {
          adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
          // Wrap with HostFragment to get separate tabbed nagivation.
          adapter.addFrag(HostFragment.newInstance(new Fragment2()), null);
          adapter.addFrag(HostFragment.newInstance(new Fragment3()), null);
          viewPager.setAdapter(adapter);
          viewPager.setOffscreenPageLimit(1);
      }
  
      public ViewPagerAdapter getAdapter() {
          return adapter;
      }
  
      public ViewPager getViewPager() {
          return viewPager;
      }
  }
  </code></pre>