简体   繁体   中英

Restoring fragment state with static variables

Usually fragment states are saved using onSaveInstanceState() and restored with onViewStateRestored() or by using a state instance fragment. However I noticed I can save fragment variable data by declaring the variables static. Obviously this won't work with view components but for primitives it seems to work fine. I wanted to know whether this static data is guaranteed to be preserved without saving it in onSaveInstanceState() .

No it is not guaranteed.

When for example Android decides to kill your process, the onSaveInstanceState() is called and the bundle will be available when you get back to your app as savedInstanceState, however all of the static variables will lose their values when your app gets killed. This applies to all of the static variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM