Get Fragment by Tag

Thomas Dang picture Thomas Dang · Jul 17, 2014 · Viewed 12.5k times · Source

I am using a FragmentPagerAdapter to create a multi-pages UI with different fragments. What I want to do is to find the Fragment using the position of the Fragment.

I have applied the method of finding the nametag of fragments and use .FindFragmentbyTag() but the .FindFragmentbyTag always return me null value. The structure seems to be correct so I am not sure why I get null values, please look through and help is appreciated.

Here is the code

MainActivity.java
public void onCreate(Bundle savedInstanceState) {
    this.getSupportFragmentManager();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MainActivity.instance=this;
    //Initialize pager
    vpPager = (ViewPager)findViewById(R.id.mypager);
    pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
    vpPager.setAdapter(pagerAdapter);
    this.mapFragment = (MapFragment)this.findFragmentByPosition(0);
    this.drFragment = (DRFragment)this.findFragmentByPosition(1);
    this.sensorFragment = (SensorFragment)this.findFragmentByPosition(2);
    ......
}

protected Fragment  findFragmentByPosition(int position) {
    int pagerid = this.vpPager.getId();
    long pagerAdapterid = this.pagerAdapter.getItemId(position);
    return getSupportFragmentManager().findFragmentByTag("android:switcher:" + pagerid + ":" + pagerAdapterid + "");
}

MyPagerAdapter.java
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;

public class MyPagerAdapter extends FragmentPagerAdapter {
    private static final String TAG = "TM_PageAdapter";
    private static int numItems = 3;

    public MyPagerAdapter(FragmentManager fragmentManager){
        super(fragmentManager);
    }

    public int getCount() {
        return numItems;
    }

    @Override
    public Fragment getItem(int position) {
        switch(position){
            case 0:
                return MapFragment.newInstance(0, "Map Information");
            case 1:
                return DRFragment.newInstance(1, "DR Information");
            case 2: 
                return SensorFragment.newInstance(2, "Sensor Information");
            default: 
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch(position){
            case 0:
                return "Map Information";
            case 1:
                return "DR Information";
            case 2: 
                return "Sensor Information";
            default:
                return null;
        }
    }

    public static String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
   }


    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

Here is one of the code of the .newInstance:

public static MapFragment newInstance(int position, String title){
    MapFragment mapFragment = new MapFragment();
    Bundle args = new Bundle();
    args.putInt("current_page", 0);
    args.putString("page_tile", "Map Information");
    mapFragment.setArguments(args);
    return mapFragment;
}

Answer

Daniel Zolnai picture Daniel Zolnai · Jul 17, 2014

In findFragmentByPosition, you are not using the index, when looking for the fragment:

getSupportFragmentManager().findFragmentByTag("android:switcher:" + pagerid + ":" + pagerAdapterid + "");

The second one should be the index, not the pagerAdapterId. So the correct code would be:

getSupportFragmentManager().findFragmentByTag("android:switcher:" + pagerid + ":" + position);

There's also no need to concatenate the last empty string.