Activity之间通过Intent传递对象
2016-11-08 19:22
381 查看
Android 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 = "Kevin"; test1.age = 12; Bundle b = new Bundle(); b.putSerializable("serial",test1); intent.putExtras(b);
B Activity 接收数据
CustomTest1 test1 = (CustomTest1) getIntent().getExtras().getSerializable("serial"); 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 = "Jhon"; Bundle b = new Bundle(); b.putParcelable("parcle",test2); intent.putExtras(b);
B Activity 接收数据
CustomTest2 test2 = getIntent().getExtras().getParcelable("parcle"); 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);
工程源码
相关文章推荐
- Android 如何通过Intent在activity之间传递自定义对象 推荐
- Android:通过Intent在两个Activity之间传递对象(Parcelable)
- 通过intent、ClipboardManager在android不同activity之间传递数据
- 用Intent 对象实现两个(多个)Activity之间的跳转和参数传递
- Activity之间通过intent 传递Map
- android Activity之间通过Bundle和Intent传递数据
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
- Activity之间通过intent 传递Map
- android传递数据方式4--通过Intent实现Activity之间的数据传递
- 通过全局对象在Activity之间传递数据
- activity 之间通过intent传递TreeMap时出现java.lang.ClassCastException: java.util.HashMap cannot be cast to jav
- Activity间通过Intent传递Bitmap对象
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
- Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法
- android中activity之间数据通过Intent的传递
- 借助Intent实现Android工程中Activity之间Java对象的传递——实现Parcelable接口
- 教你一招:在多个Activity之间传递对象,不用Bundle,Intent.putExtra
- Android中通过Intent在各组件之间传递类对象
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
- JSON:在两个activity之间使用intent传递list类型的对象