将任意对象转换为Parcelable类型竟然不损耗性能


原文地址:https://github.com/baoyongzhang/ParcelableGenerator

介绍

ParcelableGenerator可以将任意对象转换为Parcelable类型,方便对象传输。

在Android中,对象的序列化一般有两种方式,一种是Serializable,一种是Parcelable。

  • Serializable 在Java中就存在,效率较低。
  • Parcelable 是Android中提供的,也是官方推荐的方式,效率比Serializable高很多。

虽然Parcelable效率高,但是使用起来比Serializable麻烦很多,很多人不使用Parcelable就是因为写法太麻烦,尤其是属性特别多的时候,我们要将每个属性Parcel.write()然后在Parcel.read()回来,相当繁琐,不如Serializable简单粗暴,直接有效。

ParcelableGenerator可以解决Parcelable使用麻烦的问题,让使用Parcelable的简单性可以和使用Serializable相媲美。

使用方法

例如我们有一个User类,用来保存用户的一些信息,我们需要使用@Parcelable修饰该类,注意@Parcelable修饰的类必须有公有无参构造方法。


  • import com.baoyz.pg.Parcelable;
    
    @Parcelable
    public class User {
    
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
    }

我们要将一个User对象通过Intent传递给一个Activity叫做ShowUserActivity。我们需要调用Intent.putExtra()方法将对象传入,这时候直接传递肯定是不行的,我们需要调用PG.createParcelable()方法将对象转换为Parcelable在传入Intent中。

          

import com.baoyz.pg.PG;

// 模拟创建对象,并设置属性值 
User user = new User();
user.setName("zhangsan");
user.setAge(18);

Intent intent = new Intent(this, ShowUserActivity.class);
// 调用PG将对象转换成Parcelable,传入Intent中
intent.putExtra("user", PG.convertParcelable(user));
startActivity(intent);

在ShowUserActivity中获取User对象,无需写任何转换的代码,直接getIntent().getParcelableExtra()赋值给原对象类型变量即可。

  • public class ShowUserActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // 直接获取原对象类型
            User user = getIntent().getParcelableExtra("user");
    
            // 获取属性值
            user.getName();
            user.getAge();
    
        }
    
    }

    对于继承: @Parcelable 自动作用于被继承的类, 子类需修饰, 但父类无需修饰. 比如:
    public class Base {
        private String str;
    
        public String getStr() {
            return str;
        }
    
        public void setStr(String str) {
            this.str = str;
        }
    }

    注意, 上面的 Base 是没有修饰的 POD.
    @Parcelable
    public class Child extends Base {
        private int i;
    
        public int getI() {
            return i;
        }
    
        public voiid setI(int i) {
            this.i = i;
        }
    }

    注意, 子类 Child 是被 @Parcelable 修饰的.

    此时, 我们有如下代码:

    Intent intent = new Intent(this, MainActivity.class);
            Child child = new Child();
            child.setStr("child");      // 基类成员
            child.setI(1234);           // 子类成员
            intent.putExtra("bean", PG.convertParcelable(child));

    基类 str 和子类 i 两个字段均可被 Parcel.

    对于组合: 需要被组合的对象需 @Parcelable 修饰. 例如:
    @Parcelable
    public class X {
        private String str;
    
        public String getStr() {
            return str;
        }
    
        public void setStr(String str) {
            this.str = str;
        }
    }
    
    @Parcelable
    public class Y {
        private X x;
    
        public X getX() {
            return x;
        }
    
        public void setX(X x) {
            this.x = x;
        }
    }

    注意, XY 都需要 @Parcelable 修饰.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值