There is no general way to save and restore a package from persistent storage. This is due to the fact that the Parcel class does not guarantee the compatibility of Android versions. Therefore, we better not serialize it.
But if you really want to, you can sublimate the Bundle through the Parceable interface. Convert the package to a parcel (writeToParcel () / readFromParcel ()), then use the Parcel marshall () and unmarshall () methods to get the byte []. Save / load byte array to file. But there is a chance that one day you will not be able to restore your data if the user updates their Android OS to a newer version.
There is one legitimate, but very simple and unreliable way to serialize a package using ObjectOutput / InputStream. (get all the keys, iterate over the keys and save the serializable key = value pairs to a file, then read the key = pair value from the file, determine the type of value, put the data back into the Bundle using the appropriate putXXX (key, value) method). But it is not worth it)
I suggest you put your custom serializable structure in the Bundle, to save all the necessary values ββin it and save / load only this structure from the file.
Or find the best way to manage your data without using the Bundle.
source share