下面是详细讲解“Android中的Parcelable序列化对象”的完整攻略:
什么是Parcelable
Parcelable是一个序列化对象的接口,在Android中,如果我们想让一个Java对象能够在不同的组件或者进程间传递,那么这个Java对象必须去实现Parcelable接口从而达到序列化的目的。与Serializable相比,Parcelable更适用于Android,主要是因为Parcelable更加高效,可以更快的序列化和反序列化对象。
相比强制类型转换,Parcelable的最大优势在于能减少序列化后数据的大小,提高传递效率。
Parcelable 接口的使用
Parcelable接口的使用需要实现以下步骤:
- 在要传递的Java类中,实现Parcelable接口,并在实现中重写两个方法:
```
public class User implements Parcelable {
...
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.userName);
dest.writeInt(this.userId);
dest.writeString(this.userGender);
dest.writeInt(this.age);
}
}
```
describeContents()只需要返回0即可。
writeToParcel()方法需要将需要传递的Java类中的每个属性都写入到Parcel中,调用dest.writeXX()方法进行写入。其中XX代表你需要写入的属性数据类型。
- 我们需要在类中提供一个静态的Parcelable.Creator接口对象,并创建一个同名的Creator实现Parcelable.Creator接口,这个接口将在接下来的步骤中用到。
```
public static final Creator
@Override
public User createFromParcel(Parcel in) {
User user = new User();
user.userName = in.readString();
user.userId = in.readInt();
user.userGender = in.readString();
user.age = in.readInt();
return user;
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
```
这个Creator<User>系列化对象需要反序列化时使用。
- 序列化对象
在想要将数据传输到另一个组件或者进程时,将序列化对象写入Parcel中。
User user = new User();
...
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("user", user);
intent.putExtras(bundle);
startActivity(intent);
- 反序列化
在另一个组件或者进程中,需要去读取已序列化的对象:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
User user = bundle.getParcelable("user");
示例1:用户类User实现Parcelable
我们创建一个用户类User,用来展示Parcelable的用法。
public class User implements Parcelable {
private String userName;
private int userId;
private String userGender;
private int age;
public User() {}
public User(String userName, int userId, String userGender, int age) {
this.userName = userName;
this.userId = userId;
this.userGender = userGender;
this.age = age;
}
protected User(Parcel in) {
userName = in.readString();
userId = in.readInt();
userGender = in.readString();
age = in.readInt();
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.userName);
dest.writeInt(this.userId);
dest.writeString(this.userGender);
dest.writeInt(this.age);
}
}
同样我们也可以创建新类来展现如何使用Parcelable
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
User user = new User("John Smith", 120, "Male", 28);
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("user", user);
intent.putExtras(bundle);
startActivity(intent);
}
}
我们在MainActivity里面创建了一个用户对象User,将User序列化后传递给SecondActivity。SecondActivity示例代码:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
User user = bundle.getParcelable("user");
if (user != null) {
Log.d("Parcelable", user.getUserName() + " " + user.getUserId() + " " +
user.getUserGender() + " " + user.getAge());
}
}
}
我们在SecondActivity里面去反序列化对象,并打印出用户信息。
示例2:活动类Activity实现Parcelable
我们创建一个活动类Activity,重写Parcelable的方法,并尝试序列化注册生命周期中的变量。
public class ActivityTest extends AppCompatActivity implements Parcelable {
private int mActivityId;
private String mActivityName;
private int mLifecycleCount = 1;
public ActivityTest() {
mActivityId = 0;
mActivityName = "UnknownActivity";
}
public ActivityTest(int activityId, String activityName) {
mActivityId = activityId;
mActivityName = activityName;
}
protected ActivityTest(Parcel in) {
mActivityId = in.readInt();
mActivityName = in.readString();
mLifecycleCount = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mActivityId);
dest.writeString(mActivityName);
dest.writeInt(mLifecycleCount);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<ActivityTest> CREATOR = new Creator<ActivityTest>() {
@Override
public ActivityTest createFromParcel(Parcel in) {
return new ActivityTest(in);
}
@Override
public ActivityTest[] newArray(int size) {
return new ActivityTest[size];
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onStart() {
super.onStart();
mLifecycleCount++;
}
@Override
public void onResume() {
super.onResume();
mLifecycleCount++;
}
@Override
public void onPause() {
super.onPause();
mLifecycleCount++;
}
@Override
public void onStop() {
super.onStop();
mLifecycleCount++;
}
@Override
public void onDestroy() {
super.onDestroy();
mLifecycleCount++;
}
}
同样我们也可以创建新类来展现如何使用Parcelable
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityTest mActivity = new ActivityTest(1, "Main");
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("activityTest", mActivity);
intent.putExtras(bundle);
startActivity(intent);
}
}
我们在MainActivity里面创建了一个活动类ActivityTest,将ActivityTest对象序列化后传递给SecondActivity。SecondActivity示例代码:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ActivityTest mActivity = bundle.getParcelable("activityTest");
if (mActivity != null) {
Log.d("Parcelable", mActivity.mActivityName + " " + mActivity.mLifecycleCount);
}
}
}
我们在SecondActivity里面将传递过来的变量都打印出来。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android中的Parcelable序列化对象 - Python技术站