Passing parameters between tab fragments by using FragmentTabHost

434 views Asked by At

I need to pass data between tab fragments after calling switch method of tabhost mTabHost.setCurrentTab(index);

public class FragmentTabs extends FragmentActivity {
    private FragmentTabHost mTabHost;    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_tabs);
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.content);

        mTabHost.addTab(mTabHost.newTabSpec("class1").setIndicator("Class 1"),
            Class1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("class2").setIndicator("Class 2"),
            Class2.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("class3").setIndicator("Class C"),
            Class3.class, null);
    }
}

Is there any way to do that ?

1

There are 1 answers

3
Robert K. On

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

Check this: https://developer.android.com/training/basics/fragments/communicating.html