2

I want to communicate with a fragment in a FragmentTabHost

The communication Fragment->Activity is done! With an interface.

But I can't create a communication Activity->Fragment because I created the fragment like this:

mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Affichage",
                    getResources().getDrawable(android.R.drawable.star_on)),
            MySelectionFragment.class, null);

MySelectionFragment is a class not a fragment like new MySelectionFragment()

And I dunno how to communicate with a class :/

Thanks in advance!

nsvir
  • 8,781
  • 10
  • 32
  • 47
  • I can't find any method in [`TabHost`](http://developer.android.com/reference/android/widget/TabHost.html) as `addTab(TabSpec, Class, ????)`. What method did you say you used? – gunar Sep 26 '13 at 13:32
  • Neither do I. I found it in the answer: http://stackoverflow.com/questions/17227855/tabhost-with-fragments-and-fragmentactivity – nsvir Sep 26 '13 at 13:48
  • ... you're actually using a `FragmentTabHost`. Please be specific when you're posting questions. Don't lead people in the other direction. – gunar Sep 26 '13 at 14:11
  • http://stackoverflow.com/questions/14804560/fragmenttabhost-fragments-how-do-i-pass-data-between-tabs – nsvir Sep 26 '13 at 15:12

1 Answers1

2

The trick was to override the onAttach method like this:

@Override
public void onAttachFragment(android.support.v4.app.Fragment attachedFragment) {
    super.onAttachFragment(attachedFragment);

    if (attachedFragment.getClass().equals((ObjectA.class)) {
        mObjectA = (ObjectA)attachedFragment;
    }
    if (attachedFragment.getClass().equals((ObjectB.class)) {
        mObjectB = (ObjectB) attachedFragment;
    }
}
nsvir
  • 8,781
  • 10
  • 32
  • 47