FragmentTransaction show()在旋转后不适用于特定片段

时间:2014-12-04 16:56:09

标签: android android-fragments

我有一个ActionBar.TabListener的片段。在TabListener内部,如果片段已经创建,我调用显示片段。 这适用于所有片段的纵向,以及除包含pagerViewer的片段之外的所有片段的横向。对于这个有问题的片段,当手机旋转(并重新选择标签)时,不显示任何内容。

以下是我创建片段的方法(浓缩):

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.support.v4.app.FragmentActivity;
public class MainFragmentActivity extends FragmentActivity implements ResultsListener
{   
    FragmentManager fragment_manager;

@Override
protected void onCreate(Bundle savedInstanceState)
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_fragment);

    bundle = savedInstanceState;
    System.out.println("bundle = " + bundle);

    fragment_manager = getFragmentManager();

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setTitle(getApplicationContext().getApplicationInfo().labelRes);  

    ActionBar.Tab tab_jobList = actionBar.newTab().setText(R.string.stringJobs);
    tab_jobList.setIcon(R.drawable.text_list_white);

    actionBar.addTab(tab_jobList.setTabListener(new MyTabsListener<JobListLV>(this, "jobList", JobListLV.class, getFragmentManager())));
    actionBar.addTab(tab_trends.setTabListener(new MyTabsListener<TrendsPagerHolder>(this, "trendsPagerHolder", TrendsPagerHolder.class, getFragmentManager())));

}

这是具有TabListener的内部类:

    public MyTabsListener(Activity activity, String tag, Class<T> clz, Bundle args, FragmentManager fragmentManager) 
    {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;
        fm = fragmentManager;

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        Log.e("mFragment constructor", "mFragment = " + mFragment);

        fragmentTrendsPagerHolder = (TrendsPagerHolder) mActivity.getFragmentManager().findFragmentByTag("trendsPagerHolder");

        if (mFragment != null && !mFragment.isDetached()) 
        {
            FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();

            if (fragmentTrendsPagerHolder != mFragment)         
            {
                ft.hide(mFragment);
                ft.commit();
            }
        }
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) 
    {
        displayTab(tab, ft);    
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) 
    {
        if (mFragment != null) 
        {
            ft.hide(mFragment);
        }           
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) 
    {
        displayTab(tab, ft);
    }

    private void displayTab(Tab tab, FragmentTransaction ft)
    {   
        fragmentJobListDetails = mActivity.getFragmentManager().findFragmentByTag("jobDetails");

        if (fragmentJobListDetails != null && !fragmentJobListDetails.isDetached()) 
        {
            ft.hide(fragmentJobListDetails);
        }

        if (mFragment == null || !mFragment.isAdded()) 
        {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
        } 
        else 
        {       
            System.out.println("mFragment.isAdded() = " + mFragment.isAdded());
            Log.e("mFragment", "mFragment = " + mFragment);
            ft.show(mFragment);
        }   
    }
}// end MyTabsListener

有任何解决此问题的建议吗? 感谢。

1 个答案:

答案 0 :(得分:1)

在ft.show()之后调用ft.commit()。所以它应该是这样的:

private void displayTab(Tab tab)
{   
    FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();

    fragmentJobListDetails = mActivity.getFragmentManager().findFragmentByTag("jobDetails");

    if (fragmentJobListDetails != null && !fragmentJobListDetails.isDetached()) 
    {
        ft.hide(fragmentJobListDetails);
    }

    if (mFragment == null || !mFragment.isAdded()) 
    {
        mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
        ft.add(android.R.id.content, mFragment, mTag);
    } 
    else 
    {       
        System.out.println("mFragment.isAdded() = " + mFragment.isAdded());
        Log.e("mFragment", "mFragment = " + mFragment);
        ft.show(mFragment);
    }   

    ft.commit();
}

} //结束MyTabsListener

另外,创建一个新的片段事务来处理displayTab方法的可能事务。