Skip to content Skip to sidebar Skip to footer

Grab Tabtext In Fragment

I have an ActionBar that contains 11 tabs and 1 fragment. I also have a ViewPager to display the fragment(s). When a tab is clicked it creates the same fragment. The tabs display d

Solution 1:

Create a static newInstance method to create fragment and bundle the args for the fragment. Pull the args in the fragment.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


      for (String tab_name : tabs) {
            TopRatedFragment fm = new TopRatedFragment.newInstance(tab_name);
            fm.getTabs(tab_name); 
            myFragmentList.add(fm);
        }




public class TopRatedFragment extends Fragment {

public static Fragment newInstance(String position) {
    TopRatedFragment f = new TopRatedFragment();
    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putString("num", position);
    f.setArguments(args);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    Bundle b = getArguments();
    if (b!=null){
        String someText = b.containsKey("num")?b.getString("num"):null;
    }

Post a Comment for "Grab Tabtext In Fragment"