Android - Save Parcelable data into a file
Asked Answered
N

3

10

I used to use Serializable objects to save them in filesytem and read them in order to do whatever I want. But Serialization is slow when you have to pass data between activities, so I read than it's recommanded to use Parcelable. Then I did it and yeah it's faster ! But now, I have a little problem. Since Parcelable is optimized for IPC, then they aren't serializable and can't be saved into a file. So I would to know if it's possible to do it.

Also, If I decide to implement both Parcelable and Serializable interface for my class, but only use the Parcelable to pass data between my activities, I would be able to save the class into a file. But I guess than since I use serializable (only to save, not to pass data), this is not a good idea hum ?

I thought too to use Gson library, to serialize data from class, and save the JSON into a file, and reuse Gson to deserialize JSON to get my Parcelable object. Does it seems to be a good idea ? What about performance ?

Thanks to all for your answers!

J.

Naji answered 8/5, 2014 at 13:42 Comment(0)
N
7

Just do context.getFilesDir() and use a java.io.ObjectInputStream and java.io.ObjectOutputStream.

Also, with regard to "Parcelable not now serializable". This doesn't entirely make a lot of sense since Parcelable in an interface, not a class you extend.

So,

class MyClass implements Parcelable, Serializable {   
}

should work just fine. Once you read and write the object to the file system, the Parcelable interface will still work. It's only an interface.

I have to admit I haven't tried it, but it's what I wrote today and I will be writing the unit test tomorrow.

Hope this helps.

Navy answered 28/7, 2014 at 10:56 Comment(3)
Implementing both interfaces is a problem when using the class in intents, as the call putExtra() is now ambiguous - it doesn't know if you are calling the putExtra(name,parcelable) or putExtra(name, serializable)Painkiller
@Painkiller intent.putExtra(name, (Parcelable) object);Maintain
James Barwick thank you SO MUCH, your answer saved me a lot of time :DAirwoman
N
3

Here's another approach if, as you say there is a conflict between the Parcelable and Serializable interfaces. (Again, that doesn't make sense, but I'll trust you until I finish my unit tests tomorrow)...

Think about this:

    Parcel p = Parcel.obtain();

    p.writeValue(asset);

    p.setDataPosition(0);

    byte [] b = p.marshall();

    p.recycle();

OOPS, just read the javaDoc for marshall() and it says DO NOT STORE TO DISK. It also says, "Use standard serialization to store to disk" (paraphrase).

So, my first answer should do it for you.

Navy answered 28/7, 2014 at 11:8 Comment(0)
P
-5

Did you try to use shared preferences? If you need to store key values. Moreover it'll be an XML.

Ppm answered 8/5, 2014 at 13:48 Comment(5)
Thanks Robert for your reply. I thought about it yep, but if I have a big class, if I serialize it, the JSON could be big, so not sure if it's a good way to store it in SharedPreferences... I read that SharedPreferences must be used to store preferences, not really data, especially if they are big.Naji
So I guess for big data you should use databasePpm
After read your first reply, I read this : engineering.meetme.com/2014/03/… Maybe finally it's a good idea no ?Naji
IMO it's one of the fastest way, SQL database you have to create it first get access an so on, here you just create shared prefs (XML file) so less code to write, moreover there is small effort to do it so maybe you should try add some good unit tests and look how is it works? Maybe some comparation iwth reading from json file from card?Ppm
Yes, the SP is easier to use, ask less code,so I'll try a test bench to compare both and keep the best. Thanks for your help Robert!Naji

© 2022 - 2024 — McMap. All rights reserved.