Activity之间通过Intent传递对象

在Activity之间传递对象,这个对象须是可以序列化的,传递对象可以通过下面几种方式实现:

类实现 Serializable,Java 提供的序列化的接口
类实现 Parcelable,Android提供的在移动端的序列化接口,效率更高


Serializable

将类实现Serializable接口即可


  public class CustomTest1 implements Serializable{
    public String name;
    public int age;
  }
A Activity 发送数据


  Intent intent = new Intent();
  intent.setClass(this,IntentActivity2.class);
  CustomTest1 test1 = new CustomTest1();
  test1.name = "Hank";
  test1.age = 23;
  Bundle b = new Bundle();
  b.putSerializable("data",test1);
  intent.putExtras(b);
B Activity 接收数据


  CustomTest1 test1 =  (CustomTest1) getIntent().getExtras().getSerializable("data");
  if(test1 != null){
      Log.e("tag","test1 name: "+test1.name+" age: "+test1.age);
  }
Parcelable
将类实现Parcelable接口,必须重载相关的方法,必须创建一个CREATOR的变量,实现Parcelable接口,实现有点复杂


但是效率高


public class CustomTest2 implements Parcelable{
      public String name;
      public int age;




      public CustomTest2(){}




      public CustomTest2(Parcel p) {
          readFromParcel(p);
      }




      @Override
      public void writeToParcel(Parcel dest, int flags) {
          dest.writeString(name);
          dest.writeInt(age);
      }




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




      private void readFromParcel(Parcel in ) {
          name = in.readString();
          age = in.readInt();
      }




      public static final Parcelable.Creator CREATOR = new Parcelable.Creator(){
          @Override
          public CustomTest2 createFromParcel(Parcel source) {
              return new CustomTest2(source);
          }




          @Override
          public CustomTest2[] newArray(int size) {
              return new CustomTest2[size];
          }
      };
  }


A Activity 发送数据


  Intent intent = new Intent();
  intent.setClass(this,IntentActivity2.class);
  CustomTest2 test2 = new CustomTest2();
  test2.age = 14;
  test2.name = "Jack";
  Bundle b = new Bundle();
  b.putParcelable("data",test2);
  intent.putExtras(b);
B Activity 接收数据


  CustomTest2 test2 = getIntent().getExtras().getParcelable("data");
  if(null != test2){
      Log.e("tag","test2 name: "+test2.name+" age: "+test2.age);
  }
传递Bitmap对象
Activity之间可以传递bitmap对象,Bitmap的源码可以看到Bitmap已经实现了Parcelable的接口


  public final class Bitmap implements Parcelable{}
A Activity 发送数据


  Intent intent = new Intent();
  intent.setClass(this,IntentActivity2.class);

  intent.putExtra("bitmap", bitmap);


B Activity 接收数据

  Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
数据不能传递太大的数据,如果数据太大户籍引起异常,可以先对bitmap做一下压缩,然后在传递


A Activity 发送数据(压缩bitmap)


  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
  byte[] bytes = stream.toByteArray();

  intent.putExtra("bitmapbytes",bytes);

B Activity 接收数据

  byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
  Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

猜你喜欢

转载自blog.csdn.net/SophieBryant/article/details/80834254