上一篇讲了activity的创建和启动,这一篇,我们来讲讲activity的数据传递
activity之间的数据传递,这里主要介绍的是activity之间简单数据的传递,直接用bundle传递基本数据类型的数据。还有一种数据类型是parcelable和serialable
用bundle 传递数据有两种情况,这篇文章就分别从两个方面说明一下。
一、利用bundle传递基本数据类型
1、启动时传递数据,使用intent的put方法,将数据写入bundle中,然后startActivity(intent)就可以将数据到底到目标activity中去
AActivity中传递数据:
Intent intent = new Intent(AActivity.this, BActivity.class);
intent.putExtra("name", "android");
intent.putExtra("age", 20);
intent.putExtra("isStudent", true);
startActivity(intent);
BActivity中接收数据: 目标activity中通过getIntent()方法获取Intent 对象,然后就可以通过getString getInt getBoolean等方法获取到传递过来的基本数据类型的数据
String name = getIntent().getStringExtra("name");
int age = getIntent().getIntExtra("age", 0);
Boolean isStudent = getIntent().getBooleanExtra("isStudent", false);
运行结果:
2、关闭时返回数据
AActivity中启动activity
Intent intent = new Intent(AActivity.this, BActivity.class);
startActivityForResult(intent, 1);
BActivity中关闭activity并返回数据到AActivity
B中要做的工作有将要返回的数据放入intent中,然后设置返回结果码setResult(resultCode)一般结果码都是Activity.RESULT_OK,然后调用finish方法,关闭activity,返回到之前的activity
Intent intent = new Intent();
intent.putExtra("name", "这是bactivity关闭传递的数据");
setResult(Activity.RESULT_OK,intent);
finish();
接下来要在启动前的activity中接收返回的数据,重写ActivityResult方法即可,第三个参数就是带数据的intent
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case 100:
resultTxt.setText(data.getStringExtra("result"));
break;
default:
break;
}
}
}
二、利用parcelabel和serializable传递复杂数据类型
1、parcelable传递数据
如果要传递自定义的数据类型,那么将自定义数据类型实现parcelable接口即可。传递方法如下:
自定义数据类型要实现parcelable接口,然后实现两个个方法describeContents writeToParcel并且创建一个CREATOR,如下所示:
package com.yang.intentdemo;
import java.io.Serializable;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
private String name;
private int age;
private String job;
public Person(String name, int age, String job) {
super();
this.name = name;
this.age = age;
this.job = job;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", job=" + job + "]";
}
public Person() {
super();
}
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 getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(job);
// TODO Auto-generated method stub
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Person(source.readString(),source.readInt(),source.readString());
}
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return new Person[size];
}
};
}
传递对象:
在MainActivity中传递对象:
Intent intent = new Intent(MainActivity.this,ReceivActivity.class);
Person person = new Person();
person.setName("zhangsan");
person.setAge(20);
person.setJob("IOS");
intent.putExtra("person", person);
startActivity(intent);
在ReceiveActivity中取出对象:
Person person = getIntent().getParcelableExtra("person");//接收对象
if (person!=null) {
tv1.setText("接收的对象是:"+person.toString());
}
显示结果如下:
传递集合:
在MainActivity中传递集合:
Intent intent = new Intent();
ArrayList<Person> persons = new ArrayList<Person>();
Person person = new Person("wangwu", 18, "Android");
Person person1 = new Person("lisi", 26, "PHP");
Person person2 = new Person("zhaoliu", 24, "IOS");
persons.add(person);
persons.add(person1);
persons.add(person2);
intent.putParcelableArrayListExtra("persons", persons);
intent.setClass(MainActivity.this, ReceivActivity.class);
startActivity(intent);
在ReceiveActivity中取出集合:
ArrayList<Person> persons = getIntent().getParcelableArrayListExtra("persons");
if (persons!=null && persons.size()>0) {
StringBuilder sb = new StringBuilder();
for(Person personlist : persons){
sb.append(personlist.getName()+"=="+personlist.getAge()+"=="+personlist.getJob()+"\n");
}
tv2.setText(sb.toString());
}
结果显示如下:
2、serializable传递数据