How to place a location object in Parcelable

I have a Location object in one of my other Venue objects that implements Parcelable. How do I serialize this correctly in my implementation of the writeToParcel method? So here is the code:

public class Venue implements Parcelable{ public String id; public String name; public String address; public Location location; public String city; public String state; public String country; public String postalCode; @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel desc, int flags) { desc.writeString(id); desc.writeString(state); desc.writeString(name); desc.writeString(address); desc.writeString(postalCode); desc.writeString(country); desc.writeString(city); //I am still missing on a way to serialize my Location object here } } 
+6
source share
2 answers

Here you have a snippet on how to serialize transferred objects into your own parcel.

 Location location; public void writeToParcel(Parcel desc, int flags) { location.writeToParcel(desc, flags); /* do your other parcel stuff here */ } public void readFromParcel(Parcel in) { location=Location.CREATOR.createFromParcel(in); /* do your other parcel stuff here */ } 
+10
source

location = in.readParcelable(Location.class.getClassLoader()); also works.

0
source

Source: https://habr.com/ru/post/901383/


All Articles