【问题标题】:Trouble with saving fragments state after orientation is changed更改方向后无法保存片段状态
【发布时间】:2013-09-23 19:39:28
【问题描述】:

我在改变方向后无法保存当前片段的状态。 我有带有 gridview 的片段,我在片段的导航过程中替换了这些片段。 启动应用后的操作。

1) 启动“MainActivity”。

2) 添加“CenterPanelFragment”(这是一个内部片段的容器)。

3) 替换“CenterPanelFragment”片段“FragmentCatalog”(GridView)。图一。

4) 如果我点击“FragmentCatalog”的gridview 项目,它将replece 片段“FragmentCatalogStone”(Gridview)。图二。

5) 之后我在设备上更改方向,如您所见,我在横向方向中有片段“FragmentCatalog”,而不是横向方向中的片段“FragmentCatalogStone”。图3

我做错了什么? 我附上了必要的类文件。

对不起,我的英语不好。

非常感谢!

MainActivity.java

CenterPanelFragment.java

FragmentCatalog.java

FragmentCatalogStone.java

【问题讨论】:

    标签: android gridview android-fragments actionbarsherlock savestate


    【解决方案1】:

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

    【讨论】:

    • 这是一种不好的做法。如果您有 2 个不同的布局,每个方向一个,则不会重新创建活动,也不会更改布局。
    【解决方案2】:
    if (savedInstanceState != null) 
    {
        fragmentCatalog = (FragmentCatalog)getFragmentManager().getFragment(savedInstanceState, FragmentCatalog.class.getName());
    }
    else
    {
    
        fragmentCatalog = new FragmentCatalog();
    }
    

    【讨论】:

      【解决方案3】:

      我可能没有完全理解您的代码,但我怀疑您的 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();
          }
      

      【讨论】:

      • 谢谢!你说得对。更改方向后,我的 CenterPanelFragment 导致对 onActivityCreated 的新调用和 FragmentCatalog 重新创建。我在 onActivityCreated 中使用此状态,但它不起作用。:我怎样才能防止这种情况?(下面的代码)
      • 编辑了我的答案,希望对您有所帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      相关资源
      最近更新 更多