为什么Android为序列化对象提供了两个接口?序列化对象与Android Binder和AIDL文件interopt ?
当前回答
1. 可序列化的
接口是一个标记(没有抽象方法的接口),不需要重新定义任何东西。
2. Parcelable
具有抽象方法的接口。在实现它的时候,你需要重新定义所有的抽象方法,指定哪些字段以及你需要写/读的顺序(通常工作室自己可以生成它们)。
实际上没有人用Kotlin编写。对此有一个特殊的注释,由于它,这个接口的实现将自动生成。要使用它,你需要添加一个特殊的插件。
在构建。Gradle插件部分,添加另一个插件:id 'kotlin-parcelize'
同步项目
您不必担心实现方法,您所需要的只是实现Parcelable接口并添加@Parcelize注释。
一切都会好的,工作很快!
结果
如果实现Parcelable接口而不是Serializable接口,实现过程会更快。
其他回答
1. 可序列化的
@see http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
什么接口?
是标准的Java接口吗
速度
比Parcelable慢
2. Parcelable
@see http://developer.android.com/reference/android/os/Parcelable.html
什么接口?
android。操作系统接口 这意味着谷歌开发的Parcelable在android上有更好的性能
速度
更快(因为它针对android开发进行了优化)
综上所述
要知道Serializable是一个标准的Java接口,Parcelable是Android开发的接口
在Android中,我们不能只是将对象传递给活动。要做到这一点,对象必须实现Serializable或Parcelable接口。
可序列化的
Serializable是一个标准的Java接口。你可以实现Serializable接口并添加重写方法。这种方法的问题是使用了反射,而且它是一个缓慢的过程。此方法创建了大量临时对象,并导致大量垃圾收集。然而,Serializable接口更容易实现。
请看下面的例子(Serializable):
// MyObjects Serializable class
import java.io.Serializable;
import java.util.ArrayList;
import java.util.TreeMap;
import android.os.Parcel;
import android.os.Parcelable;
public class MyObjects implements Serializable {
private String name;
private int age;
public ArrayList<String> address;
public MyObjects(String name, int age, ArrayList<String> address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public ArrayList<String> getAddress() {
if (!(address == null))
return address;
else
return new ArrayList<String>();
}
public String getName() {
return name;
}
// return age
public int getAge() {
return age;
}
}
// MyObjects instance
MyObjects mObjects = new MyObjects("name", "age", "Address array here");
// Passing MyObjects instance via intent
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey");
Parcelable
Parcelable进程比Serializable快得多。原因之一是我们明确了序列化过程,而不是使用反射来推断它。出于这个目的,代码进行了大量优化,这也是合情合理的。
看看下面的例子(Parcelable):
// MyObjects Parcelable class
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class MyObjects implements Parcelable {
private int age;
private String name;
private ArrayList<String> address;
public MyObjects(String name, int age, ArrayList<String> address) {
this.name = name;
this.age = age;
this.address = address;
}
public MyObjects(Parcel source) {
age = source.readInt();
name = source.readString();
address = source.createStringArrayList();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(age);
dest.writeString(name);
dest.writeStringList(address);
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public ArrayList<String> getAddress() {
if (!(address == null))
return address;
else
return new ArrayList<String>();
}
public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() {
@Override
public MyObjects[] newArray(int size) {
return new MyObjects[size];
}
@Override
public MyObjects createFromParcel(Parcel source) {
return new MyObjects(source);
}
};
}
// MyObjects instance
MyObjects mObjects = new MyObjects("name", "age", "Address array here");
// Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getParcelableExtra("UniqueKey");
你可以传递Parcelable对象的ArrayList,如下所示:
// Array of MyObjects
ArrayList<MyObjects> mUsers;
// Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey");
结论
Parcelable接口比Serializable接口快 与可串行化接口相比,可打包接口需要更多的时间来实现 可序列化的接口更容易实现 可序列化接口创建了大量临时对象,并导致大量垃圾收集 Parcelable数组可以通过Intent在android中传递
你可以在intent中使用serializable对象,但是在使serialize成为Parcelable对象的时候,它会给出一个严重的异常,比如NotSerializableException。不建议使用serializable和Parcelable。所以最好是扩展Parcelable与你想要使用的对象捆绑和意图。由于这个Parcelable是android特定的,所以它没有任何副作用。 :)
在Parcelable中,开发人员编写用于编组和反编组的自定义代码,因此与Serialization相比,它创建的垃圾对象更少。由于这个自定义实现,Parcelable over Serialization的性能显著提高(大约快两倍)。
Serializable是一个标记接口,这意味着用户不能根据自己的需求编组数据。在序列化中,使用Java反射API在Java虚拟机(JVM)上执行编组操作。这有助于识别Java对象的成员和行为,但最终也会创建大量垃圾对象。因此,与Parcelable相比,Serialization进程较慢。
编组和反编组的含义是什么?
简而言之,“编组”是指将数据或对象转换为字节流的过程,而“解组”是将字节流转换回原始数据或对象的相反过程。转换是通过“序列化”实现的。
http://www.jguru.com/faq/view.jsp?EID=560072
在编组和解组方面存在一些性能问题。Parcelable比Serializable快两倍。
请浏览以下连结:
http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。