重新附加支持片段

时间:2015-05-06 21:40:21

标签: android android-fragments

我正在尝试重新附加我分离的支持片段。但是,在片段onCreateView中,ViewGroup为null。这是我的TabListener的相关代码:

public class TabListener implements ActionBar.TabListener {

    //Initialization stuff

    @Override
    public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) {       
    }

    @Override
    public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
        FragmentTransaction fft = mActivity.getSupportFragmentManager().beginTransaction();
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());

            mActivity.invalidateOptionsMenu();
            fft.add(android.R.id.content, mFragment, mTag);
            fft.addToBackStack(mTag);

        } else {
            // If it exists, simply attach it in order to show it
            mActivity.invalidateOptionsMenu();
            fft.attach(mFragment);
            fft.addToBackStack(mTag);
        }
        fft.commit();
    }

    @Override
    public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) {
        if (mFragment != null) {
            //Detach the fragment, because another one is being attached
            FragmentTransaction fft = mActivity.getSupportFragmentManager().beginTransaction();
            fft.detach(mFragment);
            fft.commit();
        }
    }

}

我有什么明显的遗失吗?

0 个答案:

没有答案