How do you pass a nullable list of objects with Parcelable

122
March 04, 2019, at 9:20 PM

I want to pass a custom object that has a nullable list of other custom objects. How do I go around passing this. I am using Kotlin

writeTypedList and createTypedArrayList do not work when the list is null

Answer 1

Quite easy, you just need to read the list only if it's not null.

dest.writeByte(list == null ? (byte)0 : (byte)1)
if(list != null) {
    dest.writeInt(list.size());
    dest.writeTypedList(list);
}

Then

boolean hasList = in.readByte() > 0;
if(hasList) {
    int size = in.readInt();
    List<MyObj> list = new ArrayList<>(size);
    in.readTypedList(list, MyObj.CREATOR);
}
Answer 2

How to send a list of custom objects which in turn consists of custom objects ?

You need to implement Parcelable interface for each and every custom object in the link that you have created.

dest.writeTypedList(customList) // in write to parcel 

and

this.customList = in.createTypedArrayList(CustomObject.CREATOR) // in the constructor which receives (Parcel in) as parameter

Just remember dont miss out any object from implementing parcelable interface in the link.

READ ALSO
Ionic 3 native plugin of sms not working on my device

Ionic 3 native plugin of sms not working on my device

I am using Ionic 3 and my device is 44

140
hyperlog-android not all logs are send to the server . how to solve this?

hyperlog-android not all logs are send to the server . how to solve this?

and I am initializing the hyper logs in my Base Application like this

73
Hiding / Showing Preference in PreferenceFragment without Pop-Out / -In Effect

Hiding / Showing Preference in PreferenceFragment without Pop-Out / -In Effect

We are developing an Android app that offers different feature sets to the user depending on a setting in his/her profile pageThe feature set chosen has also an effect to the apps setting page (showing more or less settings)

118
Downloading multiple images using Android Work Manager and Updating UI

Downloading multiple images using Android Work Manager and Updating UI

I am stuck at a problem where I want to download multiple images from the web (I have a list of URLs) using Work ManagerAs I am downloading those files, I need to update a progressbar on the UI and also maintain a notification in status bar

85