核心内容:
一.在 Activity 之间传递简单数据
二.在 Activity 之间传递复杂数据
三.在 Activity 之间传递自定义值对象
一.在 Activity 之间传递简单数据
二.在 Activity 之间传递复杂数据
三.在 Activity 之间传递自定义值对象
软件环境:Android Studio
一.在 Activity 之间传递简单数据
主Activity:MainActivity
Intent intent=new Intent(MainActivity.this,AnotherActivity.class); //加入参数,传递给AnotherActivity
intent.putExtra("data","我是传过来的参数"); startActivity(intent);
目标Activity:AnotherActivity
接收从主Activity传递过来的参数
getIntent().getStringExtra("data");
二.在 Activity 之间传递复杂数据
传递数据包Bundle
Intent intent=new Intent(MainActivity.this,AnotherActivity.class); Bundle b=new Bundle(); b.putString("name","小明"); b.putInt("age",20); b.putChar("sex",'男'); intent.putExtras(b); startActivity(intent);
获取数据包Bundle
Intent i=getIntent(); Bundle data=i.getExtras(); TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(String.format("name="+data.getString("name")+",age="+data.getInt("age")+",sex="+data.getChar("sex")+",score="+"99"));
三.在 Activity 之间传递自定义值对象
所谓的值对象就是自定义的有数据类型的对象,在实际使用当中传递值对象比较实用,所以这里我将着重总结一下这里。
我们新建一个数据类型Student:
这里实现了Serializable这个接口,下文中将详细讲解。
public class Student implements Serializable{ private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Student(String name,String sex,int age){ this.name=name; this.sex=sex; this.age=age; } }
当不实现这个接口,直接把数据传给Intent时,会提示出错。
这里有两种解决的方法,一种是让Student实现java内置的用于序列化的一个接口Serializable,另一种是Android提供的用于序列化的一个接口Parcelable,下面我们一个一个来看:
1.首先让Student implements Serializable接口:
主Activity中:
intent.putExtra("Student",new Student("小明","男",20));
目标Activity中:
Intent i=getIntent(); Student student= (Student) i.getSerializableExtra("Student"); //如下方式即可获取Student的属性值
String name=student.getName(); String sex=student.getSex();
int age=student.getAge();
这个方法效率比较低,所以Android提供了一个专门用于序列化的接口Parcelable,下面就来简单说说这个接口。
2.Parcelable接口
当实现这个接口之后要求我们实现这两个方法,so,实现就好。
这里需要我们手动的去写这些个东东,这是因为它没有全自动化去序列的机制。
public class Student implements Parcelable { private String name;private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(String name,int age){ this.name=name; this.age=age; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { //将这两条数据保存起来用于方便传递
parcel.writeString(getName()); parcel.writeInt(getAge()); } public static final Creator<Student> CREATOR=new Creator<Student>() { @Override public Student createFromParcel(Parcel parcel) { return new Student(parcel.readString(),parcel.readInt()); } @Override public Student[] newArray(int i) { return new Student[i]; } }; }
主Activity中:
intent.putExtra("Student",new Student("小明",20));
目标Activity中:
Intent i=getIntent(); Student student=i.getParcelableExtra("Student"); //如下方式即可获取Student的属性值
String name=student.getName(); String sex=student.getSex(); int age=student.getAge();
就这两种接口而言,Parcelable接口更快,但是很多地方需要自己写,对于像我这样的新手菜鸟来说消化它也是够费劲的,希望有大神能给点Android学习路上的经验和建议!!