Android: Any shortcut for Serializable from Parcelable without using marshall ()?

I know the performance difference between Parcelable (fast) and Serializable (slow). However, I need to store certain application information permanently, not only during one life cycle, so onSaveInstanceState and related methods using Parcelable objects are not suitable.

Therefore, I drew attention to Serializable . First of all, I have AbstractList types for storage - this is great, because they implement Serializable . However, many of the types that I store inside them are Parcelable , but not Serializable , for example. RectF .

I thought “no problem” since I can easily generate the parcel via Parcelable.writeToParcel(parcel, flags) , then call marshall() on it to create a byte[] that I can serialize and deserialize. I decided that I would use generics; create the SerializableParcelable<Parcelable> implements Serializable class, which allows a single-threaded solution for all the Parcelable types that I want to serialize. Then I, for example. save each RectF inside this shell in an ArrayList , and the lo-and-behold list and its Parcelable contents can be serialized.

However, the API docs state that marshall() should not be used for persistent storage:

public final byte [] marshall ()

Returns raw send bytes.

The data you extract here should not be placed in any permanent storage (on a local disk, on a network, etc.). To do this, you should use standard serialization or another common serialization mechanism. The Parcel marshalled view is highly optimized for local IPC and thus does not attempt to maintain compatibility with data created in different versions of the platform.

So now I'm stuck. I can either ignore this warning, or follow the route described above, or bypass the problem by expanding each individual Parcelable . I want to serialize and create special serialization methods, which seems extremely wasteful for time and effort.

Does anyone know the “right” shortcut to serialize a Parcelable object without using marshall() ? Or should I plow without listening to the warning? Maybe the SQLite database is the way to go, but I'm not sure and would like your advice.

Many thanks.

+7
source share
1 answer

For any object that needs to be serialized, you can use objectOutPutStream . Using this, you can write objects to the device’s file system. Thus, it can also be used to save Parcelable objects.

Below is the code to save the object in the File system.

  public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; FileOutputStream fileOut = null; try { File file = new File(filename); if(!file.exists()){ file.createNewFile(); } fileOut = new FileOutputStream(file,false); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); }finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } if (fileOut != null) { try { fileOut.close(); } catch (IOException e) { // do nowt } } } }` 

To read using an ObjectInputStream object. Find the code below.

  public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; FileInputStream fileIn = null; try { File file = new File(filename); fileIn = new FileInputStream(file);//context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing }catch (NullPointerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } if(fileIn != null){ try { fileIn.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return object; }` 

Yours faithfully,
Sha

-one
source

All Articles