tags:

views:

37

answers:

1

Hello Everyone,

I am trying to pass SampleParcelable class object say sampleObj from my ClassA (current) activity to ClassB (a new one), but when i log the objects value, the object's value which i am creating in ClassA is totally different from what i get in ClassB.

ClassA :-

public class ClassA extends Activity
{
    private SampleParcelable sampleObj;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        sampleObj = new SampleParcelable();
        Log.d("Actual Reference Value", "\t" + sampleObj);

        Intent terminateActivity = new Intent( ClassA.this, ClassB.class );
        terminateActivity.putExtra("SampleValue", sampleObj);
        SampleParcelable readbackCi = terminateActivity.getParcelableExtra("SampleValue");
        Log.d("Retrieved Value", "\n\n\t" + readbackCi);    
    }
}

ClassB :-

public class ClassB extends Activity
{
    private SampleParcelable newSampleObj;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        try {
            Intent intentObj = getIntent();
            Log.d("Intent Value", "intent: " + intentObj.toString());
            Log.d("Extra Values", "extras: " + intentObj.getExtras());

            newSampleObj = (SampleParcelable) intentObj.getParcelableExtra("SampleValue");

            Log.d("New Value", " " + newSampleObj.toString());
        } catch (Exception e) {
            Log.d("Exception in main", e.toString());
        }
    }
}

SampleParcelable :-

public class SampleParcelable implements Parcelable
{
    public SampleParcelable(Parcel in) {
    in.readParcelable(SampleParcelable.class.getClassLoader());
    }

    public SampleParcelable() {
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub  
    }

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

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

After debugging I guess, I know 1 reason why my object values are different, because when retrieving object in ClassB using getParcelableExtra() at that time my SampleParcelable class createFromParcel method is called which internally creates a new object. May be i m wrong.

I am not getting any solution for this, i just want same object in my new class so that i can access some values using that object which were set in my ClassA activity.

Thanks in advance

A: 

Hello there Nithalia,

Here how you can achieve what you intend for::

package com.unundoinc.FaceBook.Activity;

import android.os.Parcel;

import android.os.Parcelable;

 public class CheckParcelable implements Parcelable

{

private Facebook facebook;

public CheckParcelable() { ; }

/**
 *
 * Constructor to use when re-constructing object
 * from a parcel
 *
 * @param in a parcel from which to read this object
 */
public CheckParcelable(Parcel in) {
    readFromParcel(in);
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) 
{
    // TODO Auto-generated method stub
    dest.writeValue(getFacebook());
}

private void readFromParcel(Parcel in) {

    // readParcelable needs the ClassLoader
    // but that can be picked up from the class
    // This will solve the BadParcelableException
    // because of ClassNotFoundException
    facebook = (Facebook) in.readValue(Facebook.class.getClassLoader());

}

public void setFacebook(Facebook facebook) {
    this.facebook = facebook;
}

public Facebook getFacebook() {
    return facebook;
}

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

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

}

For Using the Parceable You Need to do something like this in the class from where you require pass the object to the other Activity::

    Facebook facebook = new Facebook();

    facebook.setAccessToken("TIMEPASS");

    CheckParcelable parcelable = new CheckParcelable();

    parcelable.setFacebook(facebook);

    Intent newIntent = new Intent(ActivityA.this, ActivityB.class);

    newIntent.putExtra("CheckParcelable", parcelable);

And for getting the Object from Other Activity you require to perform this thing ::

    CheckParcelable parcelable = getIntent().getExtras().getParcelable("CheckParcelable");

    Facebook facebook = parcelable.getFacebook();

    Log.v(TAG, "PARCELABLE IS ::" +facebook.getAccessToken());

I hope this would solve you problem ;D

y ramesh rao
Thanks alot Ramesh for answering. I tried implementing your way, but after the putExtra line (where i add my parcelable object) when i call startactivity at that time its giving me a RuntimeException saying "Parcel: unable to marshal value <classname>@435a71d0". Can you tel me how get over this exception?
Nithalia