How to set a Fragment tag by code?

前端 未结 8 2376
我寻月下人不归
我寻月下人不归 2020-12-01 02:26

I haven\'t found something like setTag(String tagName) method in the Fragment class. The only way to set a Fragment tag that I have fo

8条回答
  •  执笔经年
    2020-12-01 03:01

    I know it's been 6 years ago but if anyone is facing the same problem do like I've done:

    Create a custom Fragment Class with a tag field:

    public class MyFragment extends Fragment {
     private String _myTag;
     public void setMyTag(String value)
     {
       if("".equals(value))
         return;
       _myTag = value;
     }
     //other code goes here
    }
    

    Before adding the fragment to the sectionPagerAdapter set the tag just like that:

     MyFragment mfrag= new MyFragment();
     mfrag.setMyTag("TAG_GOES_HERE");
     sectionPagerAdapter.AddFragment(mfrag);
    

提交回复
热议问题