资料来源于网络和书籍。。。
1、使用intent 传递数据
例如:
//发送数据
Intent intent = new Intent(this,secondActivity.class);
intent.putExtra("xx",xx);
startActivty(intent);
//接受数据
//在另外一个activty中的onCreat方法中 直接getIntent
Intent intent = getIntent();
2、使用Bundle传递数据
例如:
//数据发送
Intent intent = new Intent(this,secondActivity);
Bundle bundle = new Bundle();
bundle.putBoolean("key_boolean",true);
bundle.putString("key_string","xxx");
intent.putExtra("key",bundle);//打包数据
startActivity(intent);
//数据接受
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("key");//获得数据包
Boolean xx = bundle.getBoolean("key_boolean"); //取出数据
String xx2 = bundle.getString("key_String");
3、使用Bundle传递自定义对象
传递一个实体对象,但是这个对象要实现序列化
这个可以用于在网络请求的时候的。。
//实体类 --- 必须要实现序列化Serializable
public class Person implements Serializable{
private int number; //学号
private String name; //姓名
//以下都是get、set方法
public void getNumber(){
this.number = number;
}
.......
}
//数据发送
Intent Intent =new Intent();
intent.putExtra("entity",person);//person实体类对象
startActivity(intent);
//数据接受
Person person = (Person) getIntent().getSerializableExtra("entity");
//。。。。进行其他操作