0

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 ?

Hien Nguyen
  • 744
  • 2
  • 7
  • 20

1 Answers1

2

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

Robert K.
  • 1,043
  • 1
  • 8
  • 20
  • :| I know the way 2 fragments communicate, but my question is how 2 tab fragments in FragmentTabHost communicate ? – Hien Nguyen Jun 05 '17 at 13:51
  • Then this must help you https://stackoverflow.com/questions/14804560/fragmenttabhost-fragments-how-do-i-pass-data-between-tabs – Robert K. Jun 05 '17 at 14:13
  • the method onAttachFragment(Fragment fragment) only call for the first time. I need an event be called when the tabhost switch tab and send the parameter to the tab fragment. – Hien Nguyen Jun 06 '17 at 12:45