Skip to content Skip to sidebar Skip to footer

Access Viewpager Fragment From Host Activity Returns Fragment As Null

I try to access ViewPager fragment from host activity, but MyFragment1 myFragment1 = (MyFragment1)getSupportFragmentManager().findFragmentByTag('android:switcher:'+R.id.new_pager+'

Solution 1:

You can store your fragments in a fieldvariable when they are instantiated as described HERE

Just add the Fragments to the variable in instantiateItem

@Overridepublic Object instantiateItem(ViewGroup container, int position) {
    Fragmentfragment= (Fragment) super.instantiateItem(container, position);
    registeredFragments.put(position, fragment);
    return fragment;
}

and remove them in destroyItem

@OverridepublicvoiddestroyItem(ViewGroup container, int position, Objectobject) {
    registeredFragments.remove(position);
    super.destroyItem(container, position, object);
}

Post a Comment for "Access Viewpager Fragment From Host Activity Returns Fragment As Null"