1

I have a Tab layout which has tabs added via adapter in my main activity. I need to pass a variable through the addFrag (as each frag is created per json entries). See below;

Main Activity - FragmentPagerAdapter

static class Adapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public Adapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

Main Activity - Tab Builder

    Adapter adapter = new Adapter(getSupportFragmentManager());
    adapter.addFragment(new DashboardTabs(), "Home");
    adapter.addFragment(new TeamsNewTabs(), "Teams" + _teamCount + "");
    adapter.addFragment(new FixturesNewTabs(), "Fixtures" + _fixturesCount + "");
    adapter.addFragment(new ResultsNewTabs(), "Results"+ _resultsCount + "");

    //send var through to the fragment like so ...
    //not sure how to get the "var" to even pass through ...
    String var = "my Tab Name";
    adapter.addFragment(new MyFragment(var), var);


    viewPager.setAdapter(adapter);

MyFragment - Get var when accessed.

    public class MyFragment extends Fragment {

    //not sure how to get the var or if it even passes through ...
    private getVar = var;

    }

Is there away to do this in this scenario or I am I think about it the wrong way.

BENN1TH
  • 2,003
  • 2
  • 31
  • 42

4 Answers4

0

Like this: Before commit of fragment:

Bundle b=new Bundle();
        b.putInt("cid",44);
        b.putString("title","name");
        fragment.setArguments(b);

And in Fragment:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle pb=getArguments();
        int =pb.getInt("cid");
        String title=pb.getString("title");
    }
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30
0

Pass value:

        Bundle bundle1 = new Bundle();
        bundle1.putSerializable("var","my Tab Name");

        Fragment fragment = new MyFragment();
        fragment.setArguments(bundle1);
        adapter.addFragment(fragment);

MyFragment:

         public class MyFragment extends Fragment {

         .
         .
         .
             //get value
             if(getActivity().getArguments().getStringExtra("var")!=null)
            {
                String var_value=getActivity().getArguments().getStringExtra("var");
            }

       }
Komal12
  • 3,340
  • 4
  • 16
  • 25
0

Update your method with the argument bundle. Like this:

public void addFragment(Fragment fragment, String title, Bundle args) {
    fragment.setArguments(args);
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);
 }

Now use this as:

Bundle argsBundle=new Bundle();
argsBundle.putString("data","your_data");
adapter.addFragment(new MyFragment(var), var, argsBundle); //while adding fragment.

inside onCreateView() of your MyFragment.java:

Bundle argsBundle = getActivity().getArguments();
if(argsBundle != null) {
    String var_value=argsBundle.getStringExtra("data");
}

Happy coding.

mohitum
  • 936
  • 2
  • 12
  • 26
0

Every Parcelable Object can be passed via Bundle to a fragment or an activity. Yes you can pass your value of String variable via fragment contructor but it would not be a good practice. I tell you the reason -

If Android decides to recreate your Fragment later, it's going to call the no-argument constructor of your fragment. So overloading the constructor is not a solution.

With that being said, the way to pass stuff to your Fragment so that they are available after a Fragment is recreated by Android is to pass a bundle to the setArguments method.

So, if we wanted to pass a String to the fragment we would use something like:

public static DashboardTabs newInstance(String someString) {
    DashboardTabs mF = new DashboardTabs();

    Bundle args = new Bundle();
    args.putString("key_string", someString);
    mF.setArguments(args);

    return mF;
}

and in the Fragment's onCreate() you can access that String by using:

getArguments().getString("key_string", "");

Plus point, Your Bundle will still be available if your fragment is recreated.

Finally, your addFragment method can be used as -

adapter.addFragment(DashboardTabs.newInstance(yourStringValue), "Home");

Approach is taken from - SO link and Official documentation

Community
  • 1
  • 1
Paresh P.
  • 6,677
  • 1
  • 14
  • 26