方向更改时,片段后堆栈无法恢复 [英] Fragment Backstack is not restored when orientation changes

查看:85
本文介绍了方向更改时,片段后堆栈无法恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FragmentActivity,当用户浏览应用程序时,我将当前片段替换为用户选择的片段,然后将事务添加到Backstack.一切正常,用户可以通过按后退"按钮返回到先前的片段.

I have a FragmentActivity, when the user navigates through the app, I replace the current fragment with the fragment the user has selected and add add the transactions to backstack. Everything works fine, the user can go back to the previous fragments by pressing the back button.

当设备方向改变时会发生此问题:假设用户看到的第一个片段是A,然后他导航到B,然后从B导航到C. 在横向模式下,可以更好地理解C内部的内容,并且C具有特殊的横向布局,因此用户可以旋转设备.我希望用户使用的是新的C语言.而是,用户在风景中看到了A.现在,他需要再次从A导航到B,然后再导航到C,才能在风景中看到C.

The problem occurs when the device orientation changes: suppose the first fragment that the user sees is A, then he navigates to B and from B to C. The content inside of C is better appreciated in landscape mode, and there is a special landscape layout for C, so the user rotates the device. I would expect the user to be in C with the new orientation. Instead, the user sees A in landscape. Now he needs to navigate again from A to B and then to C, to be able to see C in landscape.

我希望在方向更改后保留后退堆栈.

I want the backstack to be preserved after the orientation change.

如果我在清单中活动"的条目上使用configChange="orientation",则将保留后退堆栈,但是由于未重新创建片段,因此无法为新方向加载正确的布局.我敢打赌,发生这种情况时,我可以使用一些讨厌的技巧来迫使片段重新播放,但是我认为应该有一个更好的解决方案来实现如此简单的操作.

If I use configChange="orientation" on my Activity's entry in the manifest, the backstack is preserved, but since the fragment is not recreated, it can't load the correct layout for the new orientation. I bet I could use some nasty hack to force the recreation of the fragment when this happens, but I think there should be a better solution to achieve something so simple.

显然我做错了,因为在

Apparently I am doing something wrong, because in this page, there is an explanation that says

->片段管理器在两个 碎片.
->活动和两个片段都被破坏
->活动的新实例已创建
->使用以下方式重新创建堆栈 碎片的新实例

-> 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

听起来像我期望的行为,但这在我的应用程序中没有发生.我想念什么吗?我该如何实现这种行为?

Which sounds like the behavior I expect, but that doesn't happen in my app. Am I missing something? How can I achieve this behavior?

我在SO上看到过类似的问题,但是到目前为止,没有一个问题可以帮助我解决这个问题.任何帮助将不胜感激.

I have seen some similar questions on SO, but none of them helped me to solve this so far. Any help will be greatly appreciated.

推荐答案

原来,更改方向后会重新创建导航抽屉片段,因此它触发了onNavigationDrawerItemSelected,该片段始终替换了android正在还原的片段.我通过添加setRetainInstance(true);来修复它.在我的导航抽屉片段中.

Turns out the navigation drawer fragment was being recreated after the orientation change, so it triggered the onNavigationDrawerItemSelected, which always replaced the fragments that android was restoring. I fixed it by adding setRetainInstance(true); in my navigation drawer fragment.

这篇关于方向更改时,片段后堆栈无法恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆