Sunday, May 25, 2014

Get ViewPager's current fragment

I often want to implement dialog callback inside fragment class rather than the activity. So in the attach() method, I'll need find current fragment from activity.

Here is the way to do that:

public Fragment getCurrentFragment() {
        return getSupportFragmentManager().findFragmentByTag("android:switcher:" +
       + ":" + mViewPager.getCurrentItem());

But one culprit is that the ViewPager adapter has to be FragmentPagerAdapter not the FragmentStatePagerAdapter.

No comments: