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;
}
}