2

FragmentTabHost 内のフラグメントと通信したい

通信 Fragment->Activity ができました!インターフェース付き。

しかし、次のようにフラグメントを作成したため、通信 Activity->Fragment を作成できません。

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

MySelectionFragment はフラグメントのようなクラスではありませんnew MySelectionFragment()

そして、クラスと通信する方法がわかりません:/

前もって感謝します!

4

1 に答える 1

2

秘訣は、次のように onAttach メソッドをオーバーライドすることでした。

@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;
    }
}
于 2013-10-21T21:14:36.080 に答える