Communicate with a fragment in a FragmentTabHost

回眸只為那壹抹淺笑 提交于 2019-12-06 10:41:45

问题


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!


回答1:


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;
    }
}


来源:https://stackoverflow.com/questions/19029548/communicate-with-a-fragment-in-a-fragmenttabhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!