Android中Intent传递的四种数据类型以及传递方法

    xiaoxiao2021-03-25  169

    1、Bundle

    Bundle是将数据传递到另一个上下文中或保存,或者回复自己状态的数据存储方式,数据不是持久化状态。

    (1)简单用法

    //传递参数 Intent intentSimple = new Intent(); intentSimple.setClass(MainActivity.this,Activity2.class); Bundle bundleSimple = new Bundle(); bundleSimple.putString("usr", "xcl"); bundleSimple.putString("pwd", "zj"); intentSimple.putExtras(bundleSimple); startActivity(intentSimple); //接收参数 Bundle bunde = this.getIntent().getExtras(); String eml = bunde.getString("usr"); String pwd = bunde.getString("pwd");

    2、charsequence

    主要用来传递String,char等

    CharSequence接口,实现了这个接口的类有:CharBuffer、String、StringBuffer、StringBuilder这个四个类。 但是这个接口提供的方法有限,只有下面几个:charat、length、subSequence、toString这几个方法,感觉如果有必要,还是重载的比较好,避免用instaneof这个操作符。

    3、Serializable

    将对象序列化成二进制数据传递。

    (1)简单用法

    //传递参数 HashMap<String,String> map2 = new HashMap<String,String>(); map2.put("key1", "value1"); map2.put("key2", "value2"); Bundle bundleSerializable = new Bundle(); bundleSerializable.putSerializable("serializable", map2); Intent intentSerializable = new Intent(); intentSerializable.putExtras(bundleSerializable); intentSerializable.setClass(MainActivity.this, SerializableActivity.class); startActivity(intentSerializable); //接收参数 Bundle bundle = this.getIntent().getExtras(); ///传HashMap倒没有问题。 HashMap<String,String> map =(HashMap<String,String>)bundle.getSerializable("serializable"); String sResult = "map.size() ="+map.size(); Iterator iter = map.entrySet().iterator(); while(iter.hasNext()) { Map.Entry entry = (Map.Entry)iter.next(); Object key = entry.getKey(); Object value = entry.getValue(); sResult +="\r\n key----> "+(String)key; sResult +="\r\n value----> "+(String)value; }

    4、parcelable

    这个android提供的一种新的类型,用来封装数据的容器,和Serializable相似,但是序列化的方式不同。

    (1)实现parcelable接口

    package com.chunsoft.fragmentbackstack; import android.os.Parcel; import android.os.Parcelable; import java.util.HashMap; /** * Developer:chunsoft on 2017/3/8 14:28 * Email:chun_soft@qq.com * Content: */ public class XclParcelable implements Parcelable{ //定义要传输的数据 public int mInt; public String mStr; public HashMap<String,String> mMap = new HashMap<String,String>(); protected XclParcelable(Parcel in) { } public Creator<XclParcelable> CREATOR = new Creator<XclParcelable>() { @Override public XclParcelable createFromParcel(Parcel in) { //将映射在Parcel对象中的数据还原回来 //警告,这里顺序一定要和writeToParcel中定义的顺序一致才行!!! mInt = in.readInt(); mStr = in.readString(); mMap = in.readHashMap(HashMap.class.getClassLoader()); return new XclParcelable(in); } @Override public XclParcelable[] newArray(int size) { return new XclParcelable[size]; } }; @Override public int describeContents() { return 0; } //等于将数据映射到Parcel中去 @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(mInt); dest.writeString(mStr); dest.writeMap(mMap); } }

    (2)传递参数

    Intent intentParcelable = new Intent(); XclParcelable xp = new XclParcelable(); xp.mInt = 1; xp.mStr = "字符串"; xp.mMap = new HashMap<String,String>(); xp.mMap.put("key", "value"); intentParcelable.putExtra("Parcelable", xp); intentParcelable.setClass(MainActivity.this, ParcelableActivity.class); startActivity(intentParcelable);

    (3)接收参数

    Intent i = getIntent(); XclParcelable xp = i.getParcelableExtra("Parcelable");

    (4)parcelable和Serializable的区别

    Serializable的作用是保存对象的属性到本地文件,数据库,网络流等方便数据传输,也可程序之间传递。

    parcelable的设计的目的是为了解决Serializable效率不高的问题,内存开销小,所以在内存间传递数据的方式用parcelable,缺点是不能持久化。

    转载请注明原文地址: https://ju.6miu.com/read-1878.html

    最新回复(0)