Reliable + easy way to save / restore Android application state

In Android, you need to implement the following Activity methods so that your application can be restored to its previous state if the OS decides to destroy and then recreate your activity:

public void onSaveInstanceState(Bundle savedInstanceState)
public void onRestoreInstanceState(Bundle savedInstanceState)

In the examples I saw to implement these methods, put / getBoolean, put / getInt, etc. should be used. for a Bundle object (i.e. only primitive objects) to preserve the state of the application. This seems like an extremely error-prone way to preserve your state for starters, and I don't see how it scales to store complex objects without writing a lot of code.

What options do I have for storing / restoring state in a reliable and easy to use form?

In case this is important, my application (game) needs to store about 50 objects, each of which can contain 5 variables with a floating point and some links to the storage for other objects. I really do not want to write save / restore methods for each class and subclass (maybe about 15 of them) that I use. It would be ideal if I could just bind all my state-related objects in an object called "state" and then just call save / load on "state" to handle everything.

Does Java serialization use the option? I heard it very slowly, but is it a problem to save / restore? Can I just write my data to an SD card? To the database?

+3
2

onRetainNonConfigurationInstance getLastNonConfigurationInstance. O.S. , , . , onCreate. , , , Android. ,

.

- Application, .

0

Source: https://habr.com/ru/post/1733575/


All Articles