Android: Making a class parcelable
Here is a great tool for Android parcelable implementations.
Parcelabler by Dallas Gutauckis
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public ObjectB createFromParcel(Parcel in) {
return new ObjectB(in);
}
public ObjectB[] newArray(int size) {
return new ObjectB[size];
}
};
This field is needed for Android to be able to create new objects, individually or as arrays. This also means that you can use use the default constructor to create the object and use another method to hyrdate it as necessary.
Look at this Android – Parcelable
Online tool for creating Parcelable class
and http://www.appance.com/tag/parcelable/
You can create a parcelable class easily with NO HARD CODING approach and Android studio 4.1.2 can do it for you just in a few seconds !!!
Create a new parcelable class:
Step 1: Implement parcelable methods by pressing Alt+Enter
for windows and Option+Enter
for mac but you will find that the writeToParcel
is empty for the first time -
Step 2: Then you will find that your class name is red now. So you need to add the CREATOR
class as well as to fill the empty writeToParcel
by pressing Alt+Enter
for windows and Option+Enter
for mac -
Finally, you will see your class will be a fully parcelable class now !!!
Update an existing parcelable class:
Step 1: If you need to add some new field in your existing class then you need to remove the previous writeToParcel
and describeContents
methods and CREATOR
class and do the same steps described in the first section named Create a new parcelable class