片段事务show()第二次不显示片段

时间:2014-11-25 16:18:41

标签: android android-fragments

我在片段中有一个片段。此设置正常工作,直到您旋转设备。旋转设备后,内部片段不再显示。

我正在调用.show()方法,我有几个日志语句来显示正在进行的操作,并且片段在旋转后不会显示。

对我做错了什么的想法?

这是我的代码。可以在私有方法displayTab()中找到该问题:

public static class MyTabsListener<T extends Fragment> implements ActionBar.TabListener
{
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    private Fragment mFragment;
    private FragmentManager fm;
    private Fragment fragmentJobListDetails;
    private Fragment fragmentTrends;

    public MyTabsListener(Activity activity, String tag, Class<T> clz, FragmentManager fragmentManager) 
    {
        this(activity, tag, clz, null,fragmentManager);
    }

    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);
        if (mFragment != null && !mFragment.isDetached()) 
        {
            FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
            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);
            //ft.remove(mFragment);

        }           
    }

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

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

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

        if (mFragment == null) 
        {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
        } 
        else 
        {
            if (mFragment == fragmentTrends)
            {
                System.out.println("fragments are equal");
                System.out.println("equal. Fragment added? = " + mFragment.isAdded());
                System.out.println("equal. Fragment detached? = " + mFragment.isDetached());
                System.out.println("equal. Fragment visible? = " + mFragment.isVisible());

                ft.show(fragmentTrends);

                System.out.println("equal. Fragment added? = " + mFragment.isAdded());
                System.out.println("equal. Fragment detached? = " + mFragment.isDetached());
                System.out.println("equal. Fragment visible? = " + mFragment.isVisible());

            }

            ft.show(mFragment);
        }   
    }
}// end MyTabsListener

以下是轮换后我的日志输出:

11-25 09:17:24.895: I/System.out(14605): mFragment = TrendsPagerHolder{427a94a8 #1 id=0x1020002 trends}
11-25 09:17:24.895: I/System.out(14605): fragments are equal
11-25 09:17:24.895: I/System.out(14605): equal. Fragment added? = true
11-25 09:17:24.895: I/System.out(14605): equal. Fragment detached? = false
11-25 09:17:24.895: I/System.out(14605): equal. Fragment visible? = false
11-25 09:17:24.895: I/System.out(14605): equal. Fragment added? = true
11-25 09:17:24.895: I/System.out(14605): equal. Fragment detached? = false
11-25 09:17:24.895: I/System.out(14605): equal. Fragment visible? = false

谢谢!

2 个答案:

答案 0 :(得分:0)

我不确定整个操作有什么问题,但我使用以下几行并且它始终有效:

MyDialogFragment dialogFragment = new MyDialogFragment(data);
dialogFragment.show(getFragmentManager(), key);

请注意,MyDialogFragment有两个构造函数,一个除了数据,另一个是默认值(空)

使用saveState和restoreState可以保存对话状态。

我希望这对你也有用。

答案 1 :(得分:-1)

您需要覆盖配置更改的默认执行。您可以使用AndroidManifest中活动代码的configChanges属性来执行此操作。阅读更多相关信息here