究竟会发生什么事的Andr​​oid再现上使用嵌套的碎片和getSupportFragmentManager()改变方向时我的应用程序? [英] What exactly happens when Android recreates my app on an orientation change using nested fragments and getSupportFragmentManager()?

查看:261
本文介绍了究竟会发生什么事的Andr​​oid再现上使用嵌套的碎片和getSupportFragmentManager()改变方向时我的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了此问题相关的其他线程,他们似乎都使用getChildFragmentManager(),但如果可能的话,我想用getSupportFragmentManager()。

I've looked at other threads related to this issue and they all seem to use getChildFragmentManager() but if possible, I'd like to use getSupportFragmentManager().

下面是问题:
我有孩子持有碎片B B是一个选择片段父A片段。当用户进行选择,B是改为与C所以现在持有C.用户也可以回到B和开辟一个C显示一些其他的数据,我们把它叫做C'。当用户可以追溯到它是一种替代交易(不回栈)。此外,C和C'是相同的片段类的不同实例

Here is the issue: I have parent fragment A which holds child fragment B. B is a selector fragment. When the user makes a selection, B is "replaced" with C. So now A holds C. The user can also go back to B and open up a C displaying some other data, we'll call it C' . When the user goes back it is a "replace" transaction (not back stack). Also, C and C' are different instances of the same fragment class.

这在大多数情况下正常工作,但我发现,导致异常行为的情况。

This for the most part works fine but I found a scenario that causes abnormal behavior.


  1. 用户开始在纵向,B显示

  1. User starts in portrait, B displaying

用户在肖像,选择ç

用户切换为横向

用户在景观,C显示器

用户切换到肖像

用户在肖像,C显示器

用户在肖像,返回到B

User in portrait, returns to B

在用户写真,选择C'

用户切换为横向

用户在景观,C'显示

用户切换到肖像

用户在肖像,B显示的咦?!

User in portrait, B displays Huh?!

在#12,C'仍应显示,没有B(选择片段)。为什么不记的Andr​​oid C'在这里吗? 究竟会发生什么事的Andr​​oid使用嵌套的片段并重新上改变方向时我的应用程序getSupportFragmentManager()?

At #12, C' should still be displaying, not B (the selector fragment). Why didn't Android remember C' here? What exactly happens when Android recreates my app on an orientation change using nested fragments and getSupportFragmentManager()?

我有一个想法,它可能有一些做参考用容器ID VS标签()由getSupportFragmentManager管理的片段之间的区别。如果我使用一个容器ID,这哪是混得方向变化搞砸了,因为XML文件被交换(即使该容器的id名称并没有改变)。希望这是有道理的。我使用的容器标识的BTW。

I have a thought that it may have something to do with the difference between referencing a fragment managed by getSupportFragmentManager() using a container Id vs a tag. If I use a container Id, could this be getting screwed up on orientation changes because xml files are being swapped (even though the container id name doesnt change). Hopefully that makes sense. I am using container Id's btw.

其他的一些背景资料:我不使用后退堆栈都在这一具体情况。当我改变片段,我用的替换交易和新字保留和创造新的嵌套片段。

Some other background info: I'm not using the back stack at all in this specific situation. When I change fragments, I am using "replace" transactions and the "new" reserved word and creating a new nested fragment.

此外,这里是一个图像,以帮助您更好地理解A,B,C,C'是:

Also, here is an image to help you better understand what A, B, C, C' is:

在这里输入的形象描述

推荐答案

这个问题曾与它保持在一个方向改变旧片段,同时创造新片段的事实,我打电话super.onCreate(savedInstanceState)做时间。没想到super.onCreate(savedInstanceState)做到了。

The issue had to do with the fact that I was calling super.onCreate(savedInstanceState) which maintains old fragments on an orientation change and creating new fragments at the same time. Didn't realize super.onCreate(savedInstanceState) did that.

这篇关于究竟会发生什么事的Andr​​oid再现上使用嵌套的碎片和getSupportFragmentManager()改变方向时我的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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