Search code examples
javaandroidparcelable

Variable's value gets lost outside of the constructor in a Parcelable object


The String variable finalMapSearchUrl gets concatenated with another String (mapUrlParam) in the constructor and the Log shows the expected value : Final map URL : https://www.google.com/maps/search/?api=1&query=Spilia%20Beach (here the mapUrlParam = Spilia Beach). However, when i call the getMapSearchUrl() method from outside the class and monitor the Log, the finalMapSearchUrl's value is now back to the default https://www.google.com/maps/search/?api=1&query=. Log in getMapSearchUrl() : finalMapSearchUrl = https://www.google.com/maps/search/?api=1&query=. Any ideas on when,why and how it's value is not preserved outside of the constructor?

PlaceObject.java class:

public class PlaceObject implements Parcelable { // Implementing the Parcelable interface to allow for cleaner and faster code

    private static final String TAG = PlaceObject.class.getSimpleName();
    private static final String baseMapSearchUrl = "https://www.google.com/maps/search/?api=1&query="; // Base url for launching a Map activity with a Search Intent

    // Using int so that the values can be accessed via R.string etc.
    private int name;
    private int description;
    private int category;
    private String locationDistance;
    private String finalMapSearchUrl = baseMapSearchUrl;

    PlaceObject(int name, int description, int category , String locationDistance, String mapUrlParam) {
        this.name = name;
        this.description = description;
        this.locationDistance = locationDistance;
        this.category = category;
        finalMapSearchUrl += Uri.encode(mapUrlParam);
        Log.d(TAG,"Final map URL : " + finalMapSearchUrl);
    }

    private PlaceObject(Parcel in) {
        name = in.readInt();
        description = in.readInt();
        locationDistance = in.readString();
        category = in.readInt();
    }

    public static final Creator<PlaceObject> CREATOR = new Creator<PlaceObject>() {
        @Override
        public PlaceObject createFromParcel(Parcel in) {
            return new PlaceObject(in);
        }

        @Override
        public PlaceObject[] newArray(int size) {
            return new PlaceObject[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(name);
        parcel.writeInt(description);
        parcel.writeString(locationDistance);
        parcel.writeInt(category);
    }

    public int getName() {
        return name;
    }

    public int getDescription() {
        return description;
    }

    public String getLocationDistance() {
        return locationDistance;
    }

    public int getCategory() {
        return category;
    }

    public String getMapSearchUrl() {
        Log.d(TAG,"finalMapSearchUrl = " + finalMapSearchUrl);
        return finalMapSearchUrl; //TODO:sp figure out why the variable's value gets lost after the constructor is done
    }
} 

Solution

  • Because you're simply getting the base url and not the one Parceled.

    Solution:

    Add it to parcel and pay attention to the order of writing,

    Like this:

    private PlaceObject(Parcel in) {
            name = in.readInt();
            description = in.readInt();
            category = in.readInt();
            locationDistance = in.readString();
            finalMapSearchUrl = in.readString(); 
        }
    

    and don't forget to fix this as well:

      @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeInt(name);
            parcel.writeInt(description);
            parcel.writeInt(category);        
            parcel.writeString(locationDistance);
            parcel.writeString(finalMapSearchUrl);
        }