Android入门-Activity之间数据传递方法之Bundle

Bundle包附加数据

Bundle是一个类型容器,值只能是基本的数据类型或基本数据类型的数组,如String、int、byte、boolean等。


MainActivity

Intent intent = new Intent();

Bundle bundle = new Bundle();

bundle.putString("STRING-KEY", VALUE1); //将String类型的变量VALUE1的值插入bundle中名为STRING-KEY的key-value中

bundle.putInt("INT-KEY",VALUE2);

intent.putExtras(bundle);

intent.setClass(MainActivity.this, NewActivity.class);

startActivityForResult(intent, REQUEST-CODE);

==========================================

NewActivity

Bundle bundle = new Bundle();

bundle = getIntent().getExtras();  //getIntent()将返回对应的Intent,在这里就是MainActivity中定义的执行这一次Activity跳转的intent,getExtras()获取intent附加的bundle数据

value1 = bundle.getString();

value2 = bundle.getInt();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值