使用compatibiltiy库时setRetainInstance不工作的ListFragment [英] setRetainInstance not working for ListFragment when using compatibiltiy library

查看:135
本文介绍了使用compatibiltiy库时setRetainInstance不工作的ListFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保存ListFragment子穿过的方向变化,所以我说setRetainInstance(真),以我的onCreate函数结束。我添加了一个的onSaveInstanceState方法,它的所有的数据添加到一个包,然后加code到onActivityCreated加载的数据备份。不幸的是,这是行不通的。

I'm attempting to save a ListFragment subclass across an orientation change so I added setRetainInstance(true) to the end of my onCreate function. I added an onSaveInstanceState method to add all of it's data to a bundle, then added code into onActivityCreated to load that data back. Unfortunately, it wouldn't work.

当我加了一些调试信息与Log.d的帮助下,我发现,不仅是没有的onSaveInstanceState被调用,但的onCreate是(该文件似乎是说不该发生的时候retainInstance为true)。无论的onCreate也不onActivityCreated有束我的数据(unsuprisingly)。

When I added some debugging messages with the help of Log.d I discovered that not only was onSaveInstanceState not being called, but onCreate was (which the documentation seems to say shouldn't happen when retainInstance is true). Neither onCreate nor onActivityCreated have bundles with my data (unsuprisingly).

我猜这可能是兼容库的问题,虽然我不拥有Android 3.0+设备进行测试。

I'm guessing this may be a problem with compatibility library, though I don't have an android 3.0+ device to test this.

任何帮助是AP preciated,如有必要,我可以张贴一些code段,虽然我没有做任何事情复杂化。

Any help is appreciated and I can post some code snippets if necessary, though I'm not doing anything complicated.

更新:当我改变方向(这是应该的)的onDestroy没有被调用,因此它似乎有些setRetainInstance的正

Update: onDestroy is not being called when I change orientation (which is how it should be), so it seems that some of setRetainInstance is working

推荐答案

我终于想通了,我的问题了。这一切都归结为一个单行我忘了补充。在我FragmentActivity子我倒是压倒的onSaveInstanceState,但我从来没有叫super.onSaveInstanceState。显然,不像其他方法,他们的父母我都忘了打电话,的onSaveInstanceState不会抛出运行时错误,当我忘记调用父类的版本而已,而不是setRetainInstance刚刚停止工作。我希望这可以节省有人通过我试图解决这一去头痛。

I finally figured out what my problem was. It all came down to a single line I'd forgot to add. In my FragmentActivity subclass I'd overrode onSaveInstanceState, but I never called super.onSaveInstanceState. Apparently, unlike other methods whose parents I'd forgotten to call, onSaveInstanceState won't throw a runtime error when I forget to call the parent classes version of it, instead setRetainInstance just stops working. I hope this saves someone the headache I went through trying to solve this.

这篇关于使用compatibiltiy库时setRetainInstance不工作的ListFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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