0

I have a trouble with saving state of current fragment after changing orientation. I've got fragments with gridview that i replace in navigation process on fragments. Actions after starting app.

1) Starting "MainActivity".

2) Adding "CenterPanelFragment" (this is a container for inner fragment).

3) Replacing in "CenterPanelFragment" fragment "FragmentCatalog"(GridView). Picture 1.

4) If i click on gridview item of "FragmentCatalog", it'll replece for fragment "FragmentCatalogStone"(Gridview). Picture 2.

5) After that i change orientation on a device,as you can see i've got the fragment "FragmentCatalog" in Landscape orientation instead of fragment "FragmentCatalogStone" in Landscape orientation. Picture 3

What things i do wrong? I attached necessary class files.

Sorry for my poor english.

Thanks a lot!

1 2 3

MainActivity.java

CenterPanelFragment.java

FragmentCatalog.java

FragmentCatalogStone.java

4

3 回答 3

1

我找到了解决问题的方法。我只是android:configChanges="orientation|screenSize"在清单中为我的 Activity 添加了它,它可以工作!

于 2013-09-25T10:41:42.987 回答
0
if (savedInstanceState != null) 
{
    fragmentCatalog = (FragmentCatalog)getFragmentManager().getFragment(savedInstanceState, FragmentCatalog.class.getName());
}
else
{

    fragmentCatalog = new FragmentCatalog();
}
于 2013-09-24T05:49:43.353 回答
0

我可能没有完全理解您的代码,但我怀疑您的 MainActivity 的 onCreate 中有这一点:

    frAdapter = new FragmentAdapter(getSupportFragmentManager());

    vPager = (ViewPager)findViewById(R.id.pager);
    vPager.setAdapter(frAdapter);

    vPager.setCurrentItem(1);

    vPager.setOnPageChangeListener(this);
    vPager.setOffscreenPageLimit(3);

认为您应该将 currentItem 保存在 onSaveInstance 中并检索它:

    vPager.setCurrentItem(value_before_orientation_change);

不是 100% 确定 vPager 是否真的像我怀疑的那样覆盖了您的片段。

编辑:

更有可能的是,在 onActivityCreated 的 CenterPanelFragment.java 中提交目录:

    fragmentTransaction.replace(R.id.fragment_container, fragmentCatalog);
    fragmentTransaction.addToBackStack("FragmentCatalog");
    fragmentTransaction.commit();

也许像 CenterPanelFragment 的 onCreate 中的 setRetainInstance(true) 这样简单的东西可以解决这个问题。

我非常确信方向更改会重新启动您的 CenterPanelFragment,从而导致对 onActivityCreated 的新调用。

认为您可以在FragmentCatalog中尝试:

并更改此行:

fragmentTransaction.replace(R.id.fragment_container, fcAllStone);

至:

fragmentTransaction.replace(R.id.fragment_container, fcAllStone, "fragment_stone");

现在在CentralPanelFragment

    if (savedInstanceState != null) 
    {
        Log.d(MainActivity.tag, "CenterPanelFragment not null");

        fragmentCatalog = (FragmentCatalog)getFragmentManager().getFragment(savedInstanceState, FragmentCatalog.class.getName());

        // see if stone is open
        if (savedInstanceState != null) {
            FragmentCatalogStone fragmentStone = (FragmentCatalogStone) getFragmentManager()
                    .findFragmentByTag("fragment_stone");
        if (FragmentCatalogStone != null) {
            /*
             * maybe recommit it, dont actually think anything is needed 
             * since moving the below code inside the else statement
             * prevents it from overwriting the FragmentCatalogStone
             */
        }                  
    }
    else
    {
        Log.d(MainActivity.tag, "CenterPanelFragment null");

        fragmentCatalog = new FragmentCatalog();

         android.support.v4.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
         fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

         fragmentTransaction.replace(R.id.fragment_container, fragmentCatalog);
         fragmentTransaction.addToBackStack("FragmentCatalog");
         fragmentTransaction.commit();
    }
于 2013-09-23T20:18:46.167 回答