Android页面之间进行数据回传

要求:页面1跳转到页面2,页面2再返回页面1同时返回数据

页面1添加如下代码:

 Intent intent = new Intent();
   intent.setClass(页面1.this, 页面2.class);
   Bundle bundle = new Bundle();
   intent.putExtras(bundle);//将Bundle添加到Intent,也可以在Bundle中添加相应数据传递给下个页面,例如:bundle.putString("abc", "bbb");
   startActivityForResult(intent, 0);// 跳转并要求返回值,0代表请求值(可以随便写)

 

页面2接收数据添加代码如下:

  

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
bundle.putString("aaa", "back");//添加要返回给页面1的数据
intent.putExtras(bundle);
this.setResult(Activity.RESULT_OK, intent);//返回页面1
this.finish();

 

页面1接收返回数据:(需要重写onActivityResult)

复制代码
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            gameView.backString = bundle.getString("aaa");
             Toast.makeText(this, backString, Toast.LENGTH_SHORT).show();
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值