【问题标题】:Orientation change in Fragment. Restoring view state片段中的方向变化。恢复视图状态
【发布时间】:2013-03-10 19:06:36
【问题描述】:

我有一个片段,其中包含一些EditTexts、一些RadioGroupsSpinners

当我更改方向时,视图状态不会保留。
我尝试覆盖 onSavaInstanceState 并调用 setRetainInstance(true),但没有帮助。

我还尝试将所有插入的值保存在Bundle outState 中,如下所示:outState.putString("name", mName.getText().toString()) 并在onCreateView 中恢复它。

也无济于事,onCreateView 中的 Bundle outState 保存了这些值,但我无法操纵视图来为其设置值。比如像这样:mName.setText(savedInstanceState.getString("name"))

任何想法如何解决它?

【问题讨论】:

  • “我无法操纵视图”是什么意思?你有什么错误吗?
  • 不。它只是没有工作。我注意到,这些行为是因为我替换了 FragmentActivity 中的旧 Fragment。
  • 在 Orientation 上你得到一个片段的新实例?

标签: android fragment android-orientation orientation-changes


【解决方案1】:

我已将Fragment 替换为FragmentActivity,这导致状态未正确恢复。

我在onCreate 方法中用if (savedInstanceState == null) 包围了Fragment 提交。
我还使用了FragmentBundle outStateonSaveInstanceState 来正确恢复相同Fragment 的状态。

【讨论】:

  • 代码肯定会受到赞赏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 2014-04-25
相关资源
最近更新 更多