activity和activty之间的通信

资料来源于网络和书籍。。。

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");
//。。。。进行其他操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值