我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?


当前回答

你可以使用putExtra(Serializable..)和getSerializableExtra()方法来传递和检索你的类类型的对象;你必须将你的类标记为Serializable,并确保你所有的成员变量也是Serializable…

其他回答

We can send data one Activty1 to Activity2 with multiple ways like.
1- Intent
2- bundle
3- create an object and send through intent
.................................................
1 - Using intent
Pass the data through intent
Intent intentActivity1 = new Intent(Activity1.this, Activity2.class);
intentActivity1.putExtra("name", "Android");
startActivity(intentActivity1);
Get the data in Activity2 calss
Intent intent = getIntent();
if(intent.hasExtra("name")){
     String userName = getIntent().getStringExtra("name");
}
..................................................
2- Using Bundle
Intent intentActivity1 = new Intent(Activity1.this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putExtra("name", "Android");
intentActivity1.putExtra(bundle);
startActivity(bundle);
Get the data in Activity2 calss
Intent intent = getIntent();
if(intent.hasExtra("name")){
     String userName = getIntent().getStringExtra("name");
}
..................................................
3-  Put your Object into Intent
Intent intentActivity1 = new Intent(Activity1.this, Activity2.class);
            intentActivity1.putExtra("myobject", myObject);
            startActivity(intentActivity1); 
 Receive object in the Activity2 Class
Intent intent = getIntent();
    Myobject obj  = (Myobject) intent.getSerializableExtra("myobject");

从这个活动中启动另一个活动,通过Bundle对象传递参数

Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("USER_NAME", "xyz@gmail.com");
startActivity(intent);

检索另一个活动(YourActivity)

String s = getIntent().getStringExtra("USER_NAME");

这对于简单类型数据类型是可以的。 但是如果你想在活动之间传递复杂的数据,你需要先序列化它。

这里我们有员工模型

class Employee{
    private String empId;
    private int age;
    print Double salary;

    getters...
    setters...
}

可以使用谷歌提供的Gson lib对复杂数据进行序列化 像这样

String strEmp = new Gson().toJson(emp);
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("EMP", strEmp);
startActivity(intent);

Bundle bundle = getIntent().getExtras();
    String empStr = bundle.getString("EMP");
            Gson gson = new Gson();
            Type type = new TypeToken<Employee>() {
            }.getType();
            Employee selectedEmp = gson.fromJson(empStr, type);

另一种方法是使用Application对象(android.app.Application)。在AndroidManifest.xml文件中定义如下:

<application
    android:name=".MyApplication"
    ...

然后,您可以从任何活动调用它,并将对象保存到Application类。

在FirstActivity中:

MyObject myObject = new MyObject();
MyApplication app = (MyApplication) getApplication();
app.setMyObject(myObject);

在SecondActivity中,执行以下操作:

MyApplication app = (MyApplication) getApplication();
MyObject retrievedObject = app.getMyObject(myObject);

如果你的对象具有应用程序级别的作用域,即它们必须在整个应用程序中使用,这是很方便的。如果您希望显式控制对象范围,或者对象范围是有限的,那么Parcelable方法仍然更好。

不过,这完全避免了intent的使用。我不知道是否适合你。我使用它的另一种方式是让对象的int标识符通过intent发送,并在Application对象中检索我在Maps中拥有的对象。

如果你只是传递对象,那么Parcelable就是为这个设计的。使用它需要比使用Java的本机序列化多一点努力,但它要快得多(我的意思是快得多)。

从文档中,一个简单的例子是如何实现的:

// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
    private int mData;

    /* everything below here is for implementing Parcelable */

    // 99.9% of the time you can just ignore this
    @Override
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

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

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private MyParcelable(Parcel in) {
        mData = in.readInt();
    }
}

请注意,如果要从给定的Parcel中检索多个字段,则必须按照放入它们的相同顺序(即采用FIFO方法)执行此操作。

一旦你有你的对象实现Parcelable,这只是一个问题,把他们放入你的意图与putExtra():

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

然后你可以用getParcelableExtra()把它们拉回来:

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");

如果你的对象类实现了Parcelable和Serializable,那么请确保转换为以下类型之一:

i.putExtra("parcelable_extra", (Parcelable) myParcelableObject);
i.putExtra("serializable_extra", (Serializable) myParcelableObject);
Start another activity from this activity pass parameters via Bundle Object

Intent intent = new Intent(this, YourActivity.class);
Intent.putExtra(AppConstants.EXTRAS.MODEL, cModel);
startActivity(intent);
Retrieve on another activity (YourActivity)

ContentResultData cModel = getIntent().getParcelableExtra(AppConstants.EXTRAS.MODEL);