在android中,如何从一个activity向另外一个activity传递object?

我正在努力尝试从一个activity中发送我的自定义类中的一个object,然后显示在另外一个activity中
自定义类的代码:

public class Customer {

    private String firstName, lastName, Address;
    int Age;

    public Customer(String fname, String lname, int age, String address) {

        firstName = fname;
        lastName = lname;
        Age = age;
        Address = address;

    }

    public String printValues() {

        String data = null;

        data = "First Name :" + firstName + " Last Name :" + lastName
        + " Age : " + Age + " Address : " + Address;

        return data;

    }

}

我想要从一个activity传递object到另外一个activity然后在另外一个activity现实数据。
我怎么能够实现?

一个可能的方法是让你的自定义类实现Serializable接口,你可以用putExtra(Serializable..)通过Intent#putExtra() 方法传递对象实例
PSEUDO代码:

   intent.putExtra("MyClass", obj);  

getIntent().getSerializableExtra("MyClass");
Intent intent = new Intent();
intent.setClass(MyActivity.this,anotherActivity.class);

Bundle bundle = new Bundle();
bundle.putStringArray("MYARR", myArr);//array object
bundle.putString("TYPE", type);//string 
intent.putExtras(bundle);
startActivity(intent);
//this .finished();

类似以上方法

用Serializable实现你的类。让我们假设这是你的实体类

import java.io.Serializable;

@SuppressWarnings("serial")
public class Deneme implements Serializable {

public Deneme(double id, String name){
    this.id = id;
    this.name = name;
}

public double getId() {
    return id;
}
public void setId(double id) {
    this.id = id;
}
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}

private double id;
private String name;

}

我从Xactivity向Y activity传递这个叫dene的对象。

Deneme dene = new Deneme(4,"Mustafa");
Intent i = new Intent(this, Y.class);
i.putExtra("sampleObject", dene);
startActivity(i);

在Y activity中我获得这个对象

Intent i = getIntent();
Deneme dene = (Deneme)i.getSerializableExtra("sampleObject");

当调用一个activity

Intent intent = new Intent(fromClass.this,toClass.class).putExtra("myCustomerObj",customerObj);

在toClass.java 接收activity:

Customer customerObjInToClass = getIntent().getExtras().getParcelable("myCustomerObj");

确保自定义类实现 parcelable

 public class Customer implements Parcelable {

    private String firstName, lastName, Address;
        int Age;

    public Customer (Parcel in) {
            readFromParcel(in);
        }

        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
            public LeadData createFromParcel(Parcel in) {
                return new Customer (in);
            }

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

    @Override
        public void writeToParcel(Parcel dest, int flags) {

            dest.writeString(firstName);
            dest.writeString(lastName);
                    dest.writeString(Address);
            dest.writeInt(Age);
    }

        private void readFromParcel(Parcel in) {

            firstName= in.readString();
            lastName= in.readString();
            Address= in.readString();
            Age= in.readInt();

果然无所不用啊。 该用的方法能用的方法大家都写出来了。果然三人行必有我师!
上面的两种方法是常用的方法(在开发中),另外我说一种方法可用,但也许不是那么的实际,效率不是那么高。你可以做一个自定义类型的全局变量,在StartActivity前存,在第二个Activity的onCreate方法中去取出全局变量的值。

好的,拿上来了.

public class CanvasGlobelManager {

private static CanvasGlobelManager instance = null;
private int colorsSelectedItem = 0;
static {
    instance = new CanvasGlobelManager();
}
private CanvasGlobelManager() {
}
public static CanvasGlobelManager getInstanse() {
    return instance;
}
public synchronized int getColorsSelectedItem() {
    return this.colorsSelectedItem;
}
public synchronized void setColorsSelectedItem(int colorsSelected) {
    this.colorsSelectedItem = colorsSelected;
}
public synchronized void resetColorsSelectedItem() {
    this.colorsSelectedItem = 0;
}

}

****使用bundle类

推荐你看下这篇博客:

http://blog.csdn.net/u011403718/article/details/52689391