启动活动:
方法1:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
方法2:
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
startActivity(intent);
1.String类型与所有基本数据类型(boolean,int,float,double等)
FirstActivity中传递数据
String data = "Hello SecondActivity";
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");
FirstActivity中传递数据
int[] data={1,2,3};
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("extra_data", data);
SecondActivity中接受数据
Intent intent = getIntent();
int[] data = intent.getIntArrayExtra("extra_data");
3.类对象
通过实现Serializable接口或Parcelable借口。
自定义Staff类,实现Serializable接口,没有重写方法是因为,Serializable借口没有定义任何的抽象方法,它只是作为对象序列化的一个标志而已,没有这个标志则该对象不能实现序列化与反序列化。
public class Staff implements Serializable {
private int ID;
private String Name;
public final void setID(int value) {
ID = value;
}
public final int getID() {
return ID;
}
public final void setName(String value) {
Name = value;
}
public final String getName() {
return Name;
}
}
FirstActivity中传递数据
Staff staff=new Staff();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("staff", staff);
intent.putExtras(bundle);this.startActivity(intent);
SecondActivity中接受数据
Intent intent = getIntent();
Staff staff=(Staff)intent.getSerializableExtra("staff");
4.List
方法1:
ArrayList
传递数据
putStringArrayListExtra(String name, ArrayList value);
putIntegerArrayListExtra(String name, ArrayList value);
getStringArrayListExtra(String name);
getIntegerArrayListExtra(String name);
方法2:
传递的是List
传递数据
Intent.putExtras(key, (Serializable)list);
接受数据
(List)getIntent().getSerializable(key);