1.传递基本类型(byte、short、int、long、float、double、char、boolean) 和基本类型的数组
类型转换:
char–> 自动转换:byte–>short–>int–>long–>float–>double 强制转换:
①会损失精度,产生误差,小数点以后的数字全部舍弃。
②容易超过取值范围。
Intent intent = new Intent(this,xxx.class);
intent.putExtra("key","基本类型");
在接受的activity中
String str = getIntent().getStringExtra("key");
1.1其中getStringExtra可以换成getStringArrayExtra即可获取传递归来的字符数组;
1.2其中getStringExtra中的String可以换成其他的基本类型,同理getStringArrayExtra中的String也可换成其他的基本类型,获取该类型的数组。
2.传递Bundle(Bundle类是一个key-value对)
Bundle mBundle = new Bundle();
mBundle.putString("key",value);
Intent intent = new Intent(this,xxx.class);
intent.putExtras(mBundle);
在接受的activity中
Bundle mBundle = getIntent().getExtras();
String value = mBundle.getString("key");
3.传递对象使其序列化。
3.1 Serializable序列化的对象
//序列化对象
public class Person implements Serializable{
private String name;
private int arg;
public Person(){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
传递数据
Intent intent = new Intent(this,xxx.class);
intent.putExtra("key",new Person("小明",18));//Person是一个序列化的对象.不能传Serializable序列化的数组。
获取传递的数据
Person person = getIntent().getSerializable("key");
3.2 Parcelable序列化的对象
public class ImageDao implements Parcelable{
private String name;
private int width;
private int height;
private String path;
public ImageDao(String $Name, String $Path, int $Width, int $Height) {
height = $Height;
name = $Name;
path = $Path;
width = $Width;
}
protected ImageDao(Parcel in) {
name = in.readString();
path = in.readString();
width = in.readInt();
height = in.readInt();
//in.readList(listKey,String.class.getClassLoader());读取Arraylist
//in.readMap(map,ImageDao.class.getClassLoader());读取HashMap
}
public static final Creator<ImageDao> CREATOR = new Creator<ImageDao>() {
@Override
public ImageDao createFromParcel(Parcel in) {
return new ImageDao(in);
}
@Override
public ImageDao[] newArray(int size) {
return new ImageDao[size];
}
};
public int getHeight() {
return height;
}
public void setHeight(int $Height) {
height = $Height;
}
public String getName() {
return name;
}
public void setName(String $Name) {
name = $Name;
}
public String getPath() {
return path;
}
public void setPath(String $Path) {
path = $Path;
}
public int getWidth() {
return width;
}
public void setWidth(int $Width) {
width = $Width;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(path);
dest.writeInt(width);
dest.writeInt(height);
//dest.writeList(listKey);Arraylist
//dest.writeMap(map);HashMap
}
}
如果该ImageDao类中还有其他对象(不是基本类型),这其他对象也要实现Parcelable的序列化
传递数据
Intent intent = new Intent(this,xxx.class);
intent.putExtra("key",new ImageDao("小明",18));//ImageDao是一个序列化的对象.可以传Parcelable和序列化的数组。
获取数据
ImageDao imageDao= (ImageDao)getIntent().getParcelableArray("key");
4.传递ArrayList的数据。
Intent intent = new Intent(this,xxx.class);
intent.putIntegerArrayListExtra("key",new ArrayList());
//Integer可换成String,Parcelable,就是传递该类型的arraylist