本文主要目的是讲使用Intent传递对象,但既然讲到使用Intent传递数据,不妨先粗略总结下使用Intent传递基本类型的数据。
若读者已熟悉使用Intent传递基本类型的数据,可以直接跳到本文的重点部分-----第四部分:使用Intent传递对象
第一部分:使用Intent 的putExtra()传递基本数据类型
比如:现在要把FirstActivity中的一个字符串传递到SecondActivity中
在FirstActivity的传递数据的代码为:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data =textView.getText().toString();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("extra_data",data);
startActivity(intent);
}
});
在SecondActivity中取得数据的代码为:
Intent intent =getIntent();
String data = intent.getStringExtra("extra_data");
Log.d("SecondActivity", data);
第二部分:使用Intent结合Bundle传递数据
在FirstActivity的传递数据的代码为:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String dataxuehao = xuehao.getText().toString();
String datapassword = pwd.getText().toString();
//实例化Bundle
Bundle bundle = new Bundle();
//把数据保存到bundle里
bundle.putString("xuehao", dataxuehao);
bundle.putString("password", datapassword);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
//用putExtra传递绑定了数据的bundle
intent.putExtra("data", bundle);
startActivity(intent);
}
});
在SecondActivity中取得数据的代码为:
//获取intent
Intent intent = getIntent();
//从intent中获取bundle
Bundle bundle = intent.getBundleExtra("data");
//从bundle中取出数据
String data1 = bundle.getString("xuehao");
String data2 = bundle.getString("password");
第三部分:返回数据给上一个活动,startActivityForResult()的使用
比如:现在FirstActivity要传递一个数据"Hello" 给SecondActivity,并且希望SecondActivity销毁后会返回一个数据“Hi"给FirstActivity。
FirstActivity的代码为:
public class FirstActivity extends Activity{
private Button button;
private static int RequestCode = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity_layout);
button = (Button) findViewById(R.id.sayhello);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("hello", "Hello from FristActivity");
//RequestCode为请求码,后面在onActivityResult中会用于判断数据的来源
startActivityForResult(intent,RequestCode);
}
});
}
/**
* 对返回结果进行处理的函数,startActivityForResult启动的活动SecondActvity被销毁之后,会回调此函数
* @param requestCode 请求码,用于判断数据的来源,与startActivityForResult中的第二个参数相对应
* @param resultCode 结果码,用于判断处理结果的状态,有两个值可选RESULT_OK 和 RESULT_CANCELED
* @param data 带数据的intent
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
if (resultCode == RESULT_OK){
String returnedData = data.getStringExtra("hi");
Toast.makeText(this, returnedData, Toast.LENGTH_SHORT).show();
}
break;
}
}
}
SecondActivity的代码为:
public class SecondActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity_layout);
Toast.makeText(this, getIntent().getStringExtra("hello"), Toast.LENGTH_LONG).show();
button = (Button) findViewById(R.id.sayhi);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* 用setResult(RESULT_OK,intent)返回数据
*/
Intent intent = new Intent();
intent.putExtra("hi", "hi from SecondActivity");
//返回数据向上一个活动返回数据参数一用于标识处理结果状态,参数二为带有数据的intent
setResult(RESULT_OK,intent);
finish();//销毁活动
}
});
}
}
1.Serializable方式(将整个对象序列化)
1)在自定义对象实现Serializable接口:
public class Person implements Serializable{
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;
}
}
2)在FirstActivity中传递对象代码为:
Person person = new Person();
person.setName("Jone");
person.setAge(20);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("person_data", person);
startActivity(intent);
3)在SecondActivity获取对象中的数据代码为:
Person person = (Person) getIntent().getSerializableExtra("person_data");
String name = person.getName();
int age= person.getAge();
1)在自定义类中实现Parcelable接口并重写其中的方法
</pre><pre name="code" class="html">public class Person 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;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);//写出name
dest.writeInt(age); //写出age
}
public static final Parcelable.Creator<Person> CREATOR=new Parcelable.Creator<Person>(){
@Override
public Person createFromParcel(Parcel source) {
Person person = new Person();
person.name = source.readString(); //读取name
person.age = source.readInt(); //读取age
return person;
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
}
2)在FirstActivity中传递对象代码为(与Serializable相同):
Person person = new Person();
person.setName("Jone");
person.setAge(20);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("person_data", person);
startActivity(intent);
3)在SecondActivity获取对象中的数据代码为:
Person person = (Person) getIntent().getParcelableExtra("person_data");
String name = person.getName();
int age= person.getAge();
Serializable方式写法简单,但是由于直接序列化整个对象,效率比Parcelable方式低。
而Parcelable方式原理是把一个完整的对象分解成Intent所支持的数据类型,因此效率高些,
所以更加推荐使用的是使用Parcelable方式实现Intent传递对象的功能。