在ViewPager更新屏幕外片段 [英] Updating offscreen fragments in a ViewPager

查看:192
本文介绍了在ViewPager更新屏幕外片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了三个片段的应用程序:记录,图表,饼图。这些片段被存储在一个ViewPager在一个单一的活动时,用户可以在它们之间滑动。在记录片段具有一个表,其中用户可以输入数据。当用户输入数据时,其他两个片段需要,因为它们引用此数据被更新。本来我是想了碎片的更新,因为他们刷卡,但因为他们是滑动式我不能使用的onPause()和onResume(),因为在onPause()(因此onResume())仅被调用时,选项卡2+的屏幕了。所以我决定有记录片段发送一个回调的活动告诉其他片段来更新自己,当记录片段更新。我把所有的碎片在内存中,直到应用程序进入后台能正常工作。当该应用出来的背景,如果用户将数据输入到该表中,应用程序崩溃,因为所有的其它片段的变量都为空。这究竟是为什么?我不是在我的应用程序使用saveinstancestate任何地方。我应该是什么?

I have an application that has three fragments: Records, Chart, and PieChart. These fragments are stored in a ViewPager in a single activity and the user can swipe between them. The Records fragment has a table where the user can enter data. When the user enters data, the other two fragments need to be updated because they reference this data. Originally, I wanted the fragments to update as they were swiped to, but since they're swipeable I can't use onPause() and onResume() because onPause() (and thus onResume()) only gets called when a tab is 2+ screens away. So I decided to have the Records fragment send a callback to the Activity to tell the other fragments to update themselves when the Records fragment is updated. I keep all the fragments in memory and this works fine until the app goes into the background. When the app comes out of the background, if the user enters data into the table, the app crashes because all of the variables in the other fragments are null. Why is this happening? I am not using saveinstancestate anywhere in my app. Should I be?

推荐答案

从这里的例子中去:

http://developer.android.com/reference/android/app/ Fragment.html

您应该保存所有数据的支持下每个UI组件在你的片段onSaveInstance()的捆绑。然后在onActivityCreated()回调,恢复数据。

You should save all your data that's backing each of your UI components in the Bundle of your onSaveInstance() of your Fragment. Then in the onActivityCreated() callback, restore the data.

这篇关于在ViewPager更新屏幕外片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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