简单数据的交换:
Bundle bundle=new Bundle();
bundle.putString("name", "star");
bundle.putInt("age", 23);
intent.putExtras(bundle);
startActivity(intent);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String name=bundle.getString("name");
int age=bundle.getInt("age");
类对象的数据交换:
Person person =new Person(21, "star", "Guangzhou");
Bundle bundle=new Bundle();
bundle.putSerializable("person", person);
intent.putExtras(bundle);
startActivity(intent);
Person类(注意要实现序列化的接口):
public class Person implements Serializable{
private int age;
private String name;
private String address;
public Person(int age,String name,String address){
this.age=age;this.name=name;
this.address=address;
}
public String toString(){
return "name="+name+",age="+age+",address"+address;
}
}
提取数据传递的类信息:
Person person =(Person) intent.getSerializableExtra("person");
textView.setText(person.toString());
传递图片:
Bundle bundle=new Bundle();
Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
bundle.putParcelable("bitmap", bitmap);
intent.putExtras(bundle);
startActivity(intent);
Bitmap bitmap=intent.getParcelableExtra("bitmap");
imageView.setImageBitmap(bitmap);