Both Fragment in ActionBar return true for fragment.isVisible()


In my MainActivity with ActionBar I listen to a listener from a DialogFragment and based on which Fragment of ActionBar i am on, I need to do some things.

I am using below code to get the current fragment and check which of the two ActionBar fragment i am on:

private Fragment getVisibleFragment() { FragmentManager fragmentManager = getSupportFragmentManager(); List<Fragment> fragments = fragmentManager.getFragments(); if (fragments != null) { for (Fragment fragment : fragments) { if (fragment != null && fragment.isVisible()) return fragment; } } return null; }

but fragment.isVisible() returns true for both the fragments. Why would that be? Is there another flag I need to consider?

Below is my FragmentPagerAdapter implentation:

public class SectionsPagerAdapter extends FragmentPagerAdapter { Context context; public SectionsPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; } @Override public Fragment getItem(int index) { switch (index) { case 0: return new ReceivedListFragment(); case 1: return new SentListFragment(); } return null; } @Override public int getCount() { // Show 2 total pages. return 2; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return context.getString(R.string.title_section1).toUpperCase(l); case 1: return context.getString(R.string.title_section2).toUpperCase(l); } return null; } }


It seems you are using a ViewPager and you need to find out which is the currently selected view. The problem in your approach, in my opinion, is that the viewpager keeps instance of at least two fragments. It seems you have two fragments only, both of the fragments are kept by the viewpager and which is why you get the <strong>isVisible</strong> true for both the fragments.

If your only need is to get the currently selected fragment on the viewpager, please try this method from the viewpager class:


which will give you the currently selected fragment index.

Another way would be to add a <strong>PageChangeListener</strong> for your viewpager and then listen to page change events:

class MyActivity extends Activity{ private int curPage; private static class MFragChangeListener extends SimpleOnPageChangeListener{ public void onPageSelected(int position) { curPage = position; } } ........... /* Rest of your code */ mViewPagerInstance.setOnPageChangeListener(new MFragChangeListener()); /* Now access curPage to get the current selected page index whenever you need it. */

Please refer to <a href="https://stackoverflow.com/questions/8117523/how-do-you-get-the-current-page-number-of-a-viewpager-for-android" rel="nofollow">this</a> SO post as well.


  • Both Fragment in ActionBar return true for fragment.isVisible()
  • Android Multiple Fragments with Viewpager and ABS
  • Viewpager loose fragment connection on low memory kill
  • problems with the tabs and the fragment
  • Attach layouts to tabs - Android
  • Android FragmentStatePagerAdapter
  • Tabs + ViewPager + FragmentStatePagerAdapter - How to remove fragment?
  • how I get the current visible fragment from view pager Adapter..?
  • ViewPager with FragmentPageAdater not working properly?
  • PHP - retrieve sub array value by key
  • call asm sqrtsd under a c++
  • setCurrentItem of nested viewpager
  • how to implement factory design pattern in C#
  • FragmentPagerAdapter always load the next fragment instead the current
  • How do I get the back button to go to a certain Fragment in horizontal scrolling? Exits app right no
  • Commiting FragmentTransaction in 1 Tab of a FragmentPagerAdapter
  • Laravel validator::make vs this->validate()
  • Type mismatch: cannot convert from ListFragment to Fragment
  • Laravel: Getting Session ID oddly truncates when using foreach
  • How to disable all widgets inside Panel or inside Composite?
  • how to avoid repetitive constructor in children
  • Highlight and Bold text in JTextPane
  • how to do an event when i swipe from fragment to the other
  • sending/ receiving email in Java
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?