将同一个类实例从一个活动传递到另一个活动的问题

时间:2010-10-12 12:50:20

标签: parcelable

我正在尝试将SampleAarcelable类对象称为sampleObj从我的ClassA(当前)活动传递到ClassB(一个新的),但是当我记录对象值时,我在ClassA中创建的对象值与完全不同于我上了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];
    }
};
}

调试后我想,我知道我的对象值不同的原因,因为当时使用getParcelableExtra()在ClassB中检索对象时,我调用SampleParcelable类createFromParcel方法,在内部创建一个新对象。可能是我错了。

我没有得到任何解决方案,我只想在我的新类中使用相同的对象,以便我可以使用在我的ClassA活动中设置的该对象访问某些值。

提前致谢

1 个答案:

答案 0 :(得分:0)

在这里你如何实现你想要的目标::

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];
    }
};

}

使用Parceable你需要在类中执行类似的操作,从而将对象传递给另一个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);

要从其他活动中获取对象,您需要执行此操作::

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

    Facebook facebook = parcelable.getFacebook();

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

我希望这可以解决你的问题; D