zoukankan      html  css  js  c++  java
  • Serializable Parcelable

    Android中Intent中如何传递对象,一种是 Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口

    1.Serializable

    对象类:

    public class User implements Serializable{

        public User(int age, String name) {
            this.age = age;
            this.name = name;
        }

        public int age;
        public String name;
    }

    传递类:

    Bundle bundle = new Bundle();

    bundle.putSerializable("user", new User(26, "xiaohong"));

    Intent intent = new Intent(this, DemoActicity.class);

    intent.putExtras(bundle);

    startActivity(intent);

    接受类:

    User user = (User)getIntent().getExtras().getSerializable("user");

    Parcelable

    对象类:

    public class Student implements Parcelable{

        public int number;
        public String name;

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeInt(number);
            parcel.writeString(name);
        }

        /**
         * 1.此处必须加上 public static final
         * 2.此处变量名字必须为  CREATOR
         */
        public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
            @Override
            public Student createFromParcel(Parcel parcel) {
                Student student = new Student();
                student.number = parcel.readInt();
                student.name = parcel.readString();
                return student;
            }

            @Override
            public Student[] newArray(int i) {
                return null;
            }
        };
    }

    传递类:

    Student student = new Student();

    student.number = 1;

    student.name = "xiaohong";

    Bundle bundle = new Bundle();

    bundle.putParcelable("student", student);

    Intent intent = new Intent(this, DemoActicity.class);

    intent.putExtras(bundle);

    startActivity(intent);

    接受类:

    User user = (User)getIntent().getExtras().getParcelable("user");

  • 相关阅读:
    简单分析实现运维利器---web远程ssh终端录像回放libl
    利用kite对视频流应用进行压力测试
    Springboot 启动扩展
    SpringBoot 自动配置原理
    idea springboot没有启动项,或启动时找不到或无法加载主类
    Elasticsearch、Kibana、elasticsearch-analysis-ik 版本下载地址
    Springboot 操作Elasticsearch 方式二 【rest-high-level-client】
    Elasticsearch 安装x-pack之后,无法连接head问题
    ES版本是向下兼容的,springboot连接ES,可以用低版本客户端
    ES安装elasticsearch-head-master插件
  • 原文地址:https://www.cnblogs.com/lianghui66/p/2944589.html
Copyright © 2011-2022 走看看