Question

I am send data from one service to other service using parecable, when I write data contains proper value but when read data from parcelable it return null values.

can any one tell me what is the problem with this code?

Parcelable class

package com.airwatch.agent.finddevice;

import android.os.Parcel;
import android.os.Parcelable;


/**
 * Represents the configuration supplied from device services 
 * regarding the  find device command.
 */
public class FindDeviceConfig implements Parcelable{
    public FindDeviceConfig() {
    }

    /**
     * number of loops to play ring-tone.
     */
    private String numberoftime;

    /**
     * Delay between loops - in seconds.
     */
    private String interval;

    public FindDeviceConfig(String numberoftime, String interval) {
        this.numberoftime = numberoftime;
        this.interval = interval;
    }

    public String getNumberoftime() {
        return numberoftime;
    }

    public void setNumberoftime(String numberoftime) {
        this.numberoftime = numberoftime;
    }

    public String getInterval() {
        return interval;
    }

    public void setInterval(String interval) {
        this.interval = interval;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        //dest.writeInt(Integer.parseInt(numberoftime));

        //dest.writeInt(Integer.parseInt(interval));
        dest.writeString(this.interval);
        dest.writeString(this.numberoftime);
    }

    public static final Parcelable.Creator<FindDeviceConfig> CREATOR = new Parcelable.Creator<FindDeviceConfig>() {
        public FindDeviceConfig createFromParcel(Parcel in) {
            return new FindDeviceConfig(in);
        }

        public FindDeviceConfig[] newArray(int size) {
            return new FindDeviceConfig[size];
        }
    };

    public FindDeviceConfig(Parcel parcel)
    {
        parcel.setDataPosition(0);

        interval=parcel.readString();
        numberoftime=parcel.readString();

    }
}

Putting the parcelable object.

FindDeviceConfig config=new FindDeviceConfig("3","2");
intent.putExtra("finddevice_config", config);

Reading the parcelable object.

FindDeviceConfig config = intent.getParcelableExtra("finddevice_config");

Anyone faced this problem?

Was it helpful?

Solution

add the following constructor:

public FindDeviceConfig(Parcel data) {
    this.numberoftime  = data.readString();
    this.interval = data.readString();
 } 

OTHER TIPS

I had the same issue but a different solution, so posting here for future reference.

The most important thing to keep in mind while using Parcelable is that the order in which you write your variables to the Parcel should also be the order in which you read it.

For example,

//wirting to Parcel
public void writeToParcel(Parcel dest, int flags) {       
    dest.writeString(this.interval); //interval first
    dest.writeString(this.numberoftime); //numberoftime second
} 

//reading from Parcel 
public FindDeviceConfig(Parcel parcel)
{  
    Wrong order -- this will not work or give unwanted result

    numberoftime=parcel.readString(); 
    interval=parcel.readString();
} 

//Correct Order
public FindDeviceConfig(Parcel parcel)
{           
    interval=parcel.readString(); //interval first
    numberoftime=parcel.readString(); //numberoftime second
} 

Happy coding :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top