Get Instance of currently visible Fragment in view pager | Techbirds

There are two ways to get the instance of current visible item in view Pager.

1. First way is:

Step1: get the position of viewpager using

int item = mViewpager.getCurrentItem();

Step2: find the tag of fragment using item which you added at the time of fragment add or replace String tag = “”; if(item == 0) tag = “fragment1″; else if(item == 1)

tag = “fragment2″//fragment1 and fragment2 are the tags of Fragment1 and Fragment2

Step3: Now we can get current visible fragment instance by using findFragmentByTag();

Fragment fr = getSupportFragmentManager().findFragmentByTag(tag);

2. Second way is:

Fragment fr = getSupportFragmentManager().findFragmentByTag(“android:switcher:” + R.id.pager + “:” + mViewPager.getCurrentItem());

where R.id.pager is the id of viewpager.

Thanks, Sulabh Jain

(Android Application Developer)

10,504 total views, 1 views today

Share this Onfacebook-9066799twitter-3724347linkedin-4654468google-5048248