方向更改时不会恢复 Fragment Backstack
Fragment Backstack is not restored when orientation changes
我有一个 FragmentActivity
,当用户浏览应用程序时,我将当前片段替换为用户选择的片段,并将事务添加到后台堆栈。一切正常,用户可以通过按后退按钮返回到之前的片段。
当设备方向改变时出现问题:假设用户看到的第一个片段是A,然后他导航到B,然后从B导航到C。
C 里面的内容在横向模式下更好地欣赏,并且 C 有一个特殊的横向布局,所以用户旋转设备。我希望用户在 C 中使用新的方向。相反,用户看到的是横向的 A。现在他需要再次从 A 导航到 B,然后再导航到 C,以便能够横向看到 C。
我希望在方向更改后保留后退堆栈。
如果我在清单中的 Activity 条目上使用 configChange="orientation"
,则后退堆栈会保留,但由于未重新创建片段,因此无法加载正确的布局新方向。我敢打赌,当发生这种情况时,我可以使用一些讨厌的 hack 来强制重新创建片段,但我认为应该有更好的解决方案来实现如此简单的事情。
显然我做错了什么,因为在 this page 中有一个解释说
-> Fragment Manager invokes onSaveInstanceState() on both the
fragments.
-> Activity and both Fragments are destroyed
-> New instance of the Activity is created
-> Back stack is recreated with
new instances of the Fragments
这听起来像我期望的行为,但在我的应用程序中并没有发生。我错过了什么吗?我怎样才能实现这种行为?
我在 SO 上看到过一些类似的问题,但到目前为止 none 帮助我解决了这个问题。任何帮助将不胜感激。
我认为您没有检查 adding/replacing 片段之前的 savedInstanceState。试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check that the activity is using the layout version
// with the fragment_container_main FrameLayout
if (findViewById(R.id.fragment_container_main) != null) {
// If we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) { //Do nothing, Android has got your back.
} else {
FragmentOne fragmentOne = new FragmentOne();
// Add the fragment to the fragment_container_main FrameLayout
getFragmentManager().beginTransaction()
.add(R.id.fragment_container_main,
fragmentOne,
"FRAG_ONE").commit();
}
}
}
事实证明导航抽屉片段在方向更改后被重新创建,因此它触发了 onNavigationDrawerItemSelected,它总是替换 android 正在恢复的片段。我通过添加 setRetainInstance(true) 修复了它;在我的导航抽屉片段中。
我有一个 FragmentActivity
,当用户浏览应用程序时,我将当前片段替换为用户选择的片段,并将事务添加到后台堆栈。一切正常,用户可以通过按后退按钮返回到之前的片段。
当设备方向改变时出现问题:假设用户看到的第一个片段是A,然后他导航到B,然后从B导航到C。 C 里面的内容在横向模式下更好地欣赏,并且 C 有一个特殊的横向布局,所以用户旋转设备。我希望用户在 C 中使用新的方向。相反,用户看到的是横向的 A。现在他需要再次从 A 导航到 B,然后再导航到 C,以便能够横向看到 C。
我希望在方向更改后保留后退堆栈。
如果我在清单中的 Activity 条目上使用 configChange="orientation"
,则后退堆栈会保留,但由于未重新创建片段,因此无法加载正确的布局新方向。我敢打赌,当发生这种情况时,我可以使用一些讨厌的 hack 来强制重新创建片段,但我认为应该有更好的解决方案来实现如此简单的事情。
显然我做错了什么,因为在 this page 中有一个解释说
-> Fragment Manager invokes onSaveInstanceState() on both the fragments.
-> Activity and both Fragments are destroyed
-> New instance of the Activity is created
-> Back stack is recreated with new instances of the Fragments
这听起来像我期望的行为,但在我的应用程序中并没有发生。我错过了什么吗?我怎样才能实现这种行为?
我在 SO 上看到过一些类似的问题,但到目前为止 none 帮助我解决了这个问题。任何帮助将不胜感激。
我认为您没有检查 adding/replacing 片段之前的 savedInstanceState。试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check that the activity is using the layout version
// with the fragment_container_main FrameLayout
if (findViewById(R.id.fragment_container_main) != null) {
// If we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) { //Do nothing, Android has got your back.
} else {
FragmentOne fragmentOne = new FragmentOne();
// Add the fragment to the fragment_container_main FrameLayout
getFragmentManager().beginTransaction()
.add(R.id.fragment_container_main,
fragmentOne,
"FRAG_ONE").commit();
}
}
}
事实证明导航抽屉片段在方向更改后被重新创建,因此它触发了 onNavigationDrawerItemSelected,它总是替换 android 正在恢复的片段。我通过添加 setRetainInstance(true) 修复了它;在我的导航抽屉片段中。