Android中传递数据(Activity、Fragment)
程序员文章站
2022-05-14 17:53:31
...
对于复杂数据,比如传递实体类,有两种方式,java.io.Serializable和eandroid.os.Parcelable,其中Parcelable的性能比Serializable好,节省内存,但是写起来会稍复杂些,所以有些时候为了方便就选择前一种方式。
Serializable方式
一.将要传递的实体类实现Serializabl即可,
public static class CommentsBean implements Serializable {}
2.在activity中使用
RankCommentFragment rankCommentFragment = RankCommentFragment.newInstance();
bundle = new Bundle();
BookRankDetailsBean.CommentsBean comments = bookRankDetailsBean.getComments();
bundle.putSerializable("comment", comments);
rankCommentFragment.setArguments(bundle);
fragments.add(rankCommentFragment);
3.fragment中接收:
Bundle arguments = getArguments();
if (arguments != null) {
BookRankDetailsBean.CommentsBean comment = (BookRankDetailsBean.CommentsBean) arguments.getSerializable("comment");
rankCommentData.tvRankcomment.setText("最新评论("+comment.getLatest().size()+")");
}
Parcelable方式
一.Bean类实现Parcelable接口
public static class SectionsBean implements Parcelable {
private SectionBean section;
private PricingBeanDes pricing;
private AccountBeanDes account;
private StatBeanDes stat;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(this.section, flags);//把实体类写入Parcel中
}
public SectionsBean() {
}
protected SectionsBean(Parcel in) {
this.section = in.readParcelable(SectionBean.class.getClassLoader());//读取序列化的类对象的加载器
}
public static final Parcelable.Creator<SectionsBean> CREATOR = new Parcelable.Creator<SectionsBean>() {
@Override
public SectionsBean createFromParcel(Parcel source) {
return new SectionsBean(source);//创建实例
}
@Override
public SectionsBean[] newArray(int size) {
return new SectionsBean[size];
}
};
public static class SectionBean implements Parcelable {
private int id;
private int section_number;
private String title;
private String description;
private int length;
private int file_size;
protected SectionBean(Parcel in) {//读取对象
id = in.readInt();
section_number = in.readInt();
title = in.readString();
description = in.readString();
length = in.readInt();
file_size = in.readInt();
}
public static final Creator<SectionBean> CREATOR = new Creator<SectionBean>() {
@Override
public SectionBean createFromParcel(Parcel in) {//创建类的实例读取对象,读写顺序必须一样,不然会出现数据紊乱
return new SectionBean(in);
}
@Override
public SectionBean[] newArray(int size) {
return new SectionBean[size];
}
};
@Override
public int describeContents() {
return 0;//内容接口描述,默认返回0就行
}
@Override
public void writeToParcel(Parcel dest, int flags) {//将我们的对象序列化成一个Parcel对象(存入Parcel中)
dest.writeInt(id);
dest.writeInt(section_number);
dest.writeString(title);
dest.writeString(description);
dest.writeInt(length);
dest.writeInt(file_size);
}
}
SectionBean中是一些基本的变量,如果外面还有一层实体类,因为有时候我们需要传递的是一个大的实体类,分别取里面几个Bean的值,写法有一点不同,但其中逻辑都是一样的。
二.传递时,和普通的intent传值一样:
Intent intent = new Intent(context, AudioPlayActivity.class);
BookRankDetailsBean.SectionsBean sectionsBean = sectionBeen.get(position);
intent.putExtra("sectionsBean", sectionsBean);
context.startActivity(intent);
三.Activity或者Fragment中接收:
Intent intent = getIntent();
BookRankDetailsBean.SectionsBean sectionsBean = intent.getParcelableExtra("sectionsBean");
就是在Bean中序列化复杂一点,传递时还是很简单的。由于每次实现Parcelable过程都是一样的,所以studio有个插件可以帮我们完成这繁琐的工作
点击File->Settings->Plugins,搜索Parcelable
安装之后重启stduio,在Bean类中使用alt+insert快速出现Generate快捷键,选择Parcelable确认即可。