Answer the question
In order to leave comments, you need to log in
Is it possible to save collections of objects in a Bundle (onSaveInstanceState(Bundle))?
For example, I have a Queue collection containing instances of the Word class, is it possible to push it into a Bundle so that I can get it from there when I recreate the activity? Or is it possible to save only primitive instances or collections of strings?
Answer the question
In order to leave comments, you need to log in
Use the `putSerializable(String, Serializable)` method.
Your Queue must implement the Serializable interface and then it can be pushed into the Bundle.
As far as I remember, the object in the queue must also be Serializable.
Bundle bundle = new Bundle();
Queue<Word> queue = new LinkedBlockingQueue();
bundle.putSerializable("my_queue", queue);
Well, in the case of the queue. - I'd rather use the Parcelable element. it is just possible and convenient to restore the entire queue. and the cost of object serialization can be avoided.
Another option is to implement the "Queue" data structure as a database table (the logic of this simple structure is in a separate class) and no serialization is needed, we work with the class as a wrapper. Access via b/w stat. class methods, or inject through DI (Dagger2 for example).
PS if anyone has objections, write a comment, it's interesting to discuss.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question