问题描述
我正在构建一个由带选项卡的操作栏组成的应用程序,每个选项卡均由一个片段组成.片段之一具有需要加载的数据.因此,我希望仅在第一次加载数据.当用户更改选项卡并返回到包含该片段的选项卡时,无需等待数据再次加载.我假设我必须使用某种保存数据的方式,以便下次用户返回该特定片段时可以使用该数据.但是我只是不知道应该使用哪种方法.如果有人知道或可以提出保存片段状态的示例,请告诉我.谢谢你!
I am building an app that consists of actionbar with tabs, and each tab consists of a fragment. One of the fragments has data that need to be loaded. So I want the data to be loaded only the first time. When the user changes the tabs and goes back to the one containing that fragment, doesn't wait for the data to be loaded again. I assume I have to use some kind of saving the data so that it can be used the next time the user goes back to that particular fragment. But I just do not know what method I should use. If someone knows or can suggest an example of saving fragment state, please let me know. Thank u!
推荐答案
尝试一下:
myFragment.setRetainInstance(true);
这意味着不会在每次添加片段时都重新创建该片段.
It'll mean the fragment isn't recreated every time it's added.
这篇关于保存片段状态操作栏选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!