我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?
当前回答
如果你对putExtra特性的使用不是很特别,只是想启动另一个带有对象的活动,你可以查看我写的GNLauncher (https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher)库,它试图使这个过程更直接。
GNLauncher可以将对象/数据从另一个活动发送到一个活动,就像调用活动中的函数一样简单,并将所需的数据作为参数。它引入了类型安全,并消除了必须序列化的所有麻烦,使用字符串键附加到意图,并在另一端撤销相同的操作。
其他回答
您需要将对象序列化为某种字符串表示形式。一种可能的字符串表示是JSON,如果你问我,在android中序列化JSON最简单的方法之一是通过谷歌GSON。
在这种情况下,你只需要输入字符串返回值from (new Gson()).toJson(myObject);并检索字符串值并使用fromJson将其转换回您的对象。
但是,如果您的对象不是非常复杂,那么可能不值得这样做,您可以考虑传递对象的单独值。
如果你只是传递对象,那么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);
你可以通过intent发送可序列化对象
// send where details is object
ClassName details = new ClassName();
Intent i = new Intent(context, EditActivity.class);
i.putExtra("Editing", details);
startActivity(i);
//receive
ClassName model = (ClassName) getIntent().getSerializableExtra("Editing");
And
Class ClassName implements Serializable {
}
对于你知道要在应用程序中传递数据的情况,使用“全局变量”(比如静态类)
以下是Dianne Hackborn (hackbod -谷歌安卓软件工程师)对此事的看法:
For situations where you know the activities are running in the same process, you can just share data through globals. For example, you could have a global HashMap<String, WeakReference<MyInterpreterState>> and when you make a new MyInterpreterState come up with a unique name for it and put it in the hash map; to send that state to another activity, simply put the unique name into the hash map and when the second activity is started it can retrieve the MyInterpreterState from the hash map with the name it receives.
你的类应该实现Serializable或Parcelable。
public class MY_CLASS implements Serializable
一旦完成,你可以在putExtra上发送一个对象
intent.putExtra("KEY", MY_CLASS_instance);
startActivity(intent);
要得到额外的,你只需要做
Intent intent = getIntent();
MY_CLASS class = (MY_CLASS) intent.getExtras().getSerializable("KEY");
如果您的类实现了Parcelable,请使用下一个
MY_CLASS class = (MY_CLASS) intent.getExtras().getParcelable("KEY");
我希望它能帮到你
推荐文章
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?