Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Save Parcelable data into a file

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.

like image 424
ejay Avatar asked May 08 '14 13:05

ejay


People also ask

How to implement parcelable in Android Studio?

If you’re still using Java in your project, you can rely on Android Studio to write all necessary codes for you. After implementing Parcelable in your class, move your mouse on the class name, press Alt+Entre, and select Add Parcelable Implementation option. By doing this, Android Studio writes all required codes for you.

How to save application data in Android system?

The Android system provides different options to save application data: Let us go through these all one by one. 1. Android Application-specific storage In this, the stored data is meant only for a specific application’s use. It is stored either in a dedicated directory inside the internal storage or in external storage.

What is the difference between parcelable reflection and serializable in Android?

Unlike Serializable, in Parcelable reflection won’t be used so it is faster and has better performance Android Parcelabe is not made to save objects into the files, so if you want to save an object in the file you must use Serializable

Where are files saved on Android phone?

Internal Storage in Android Files can be saved directly in the “internal” storage of the device. When the files are saved in the internal storage, they are automatically set to private. These files are set to private so they cannot be used by other applications.


1 Answers

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.

like image 112
James Barwick Avatar answered Oct 28 '22 00:10

James Barwick