saving fragment state actionbar tab

2019-05-14 11:24发布

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!

1条回答
看我几分像从前
2楼-- · 2019-05-14 11:45

Try this:

myFragment.setRetainInstance(true);

It'll mean the fragment isn't recreated every time it's added.

查看更多
登录 后发表回答