A
A
Alexander Vasilenko2015-11-27 07:58:46
Android
Alexander Vasilenko, 2015-11-27 07:58:46

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

3 answer(s)
L
LeEnot, 2015-11-27
@SanchelliosProg

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);

However, the cost of serializing "heavy" objects can be quite high.

A
Alexey Perelygin, 2015-11-27
@orcDamnar

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.

R
Rustem Saitkulov, 2015-11-28
@atetc

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 question

Ask a Question

731 491 924 answers to any question