Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

readFromParcel() method in Parcelable interface

I have a simple doubt in marshaling during service creation. When there is a writeToParcel() method declared in Parcelable interface which is invoked in stub generated (if aidl method parameters are declared as in), why there is no readFromParcel() declaration in Parcelable interface(for out parameters)?

I can create my own readFromParcel() but as per my understanding there should be a overridden readFromParcel() declaration in Parcelable interface if the generated stub wants to invoke it. But the documentation for Parcelable interface does not show any sign of readFromParcel() method. Why is it so? Was it included in previous API version and later got removed? Please explain !

And how different is createFromParcel() from readFromParcel() if both tries to read a parcelable object and populate member fields with the data out of it?

like image 327
Babu Prasad Avatar asked Jul 21 '14 15:07

Babu Prasad


People also ask

What is a primary purpose of the Parcelable interface?

The Parcelable interface adds methods to all classes you want to be able to transfer between activities. These methods are how parcelable deconstructs the object in one activity and reconstructs it in another. For this example you'll look at how to implement parcelable in a simple class.

How do you implement Parcelable?

Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.

What is Parcelable Android example?

Parcelable is a serialization mechanism provided by Android to pass complex data from one activity to another activity.In order to write an object to a Parcel, that object should implement the interface “Parcelable“.

How do you make a class Parcelable in Java?

If a developer wants to convert a Java object into Parcelable, then the best way to do so is by implementing the Parcelable interface and overriding the writeToParcel() methods in its own class. The first step is to override the writeToParcel() method and write all object members into parcel objects.


2 Answers

This is because you have declared a parameter of that type as "inout" in your AIDL.

When returned from the method, generated AIDL proxy will call readFromParcel() to update the parameter value (as defined by the "inout" qualifier).

like image 165
Oasis Feng Avatar answered Oct 10 '22 21:10

Oasis Feng


createFromParcel is exactly what it sounds like. A NEW Intance of the parcelable Object/Class that has been written to parcel : Parcelable.writeToParcel() is created. This is a good thing, as it helps prevent memory leaks, as you are not holding on to a reference to the object from another class that may or may not have been destroyed

like image 36
erik Avatar answered Oct 10 '22 19:10

erik