android数据回传多个页面_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();
}
}
總結
以上是生活随笔為你收集整理的android数据回传多个页面_Android页面之间进行数据回传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 相机成像原理_【亲子科学小实验】相机原理
- 下一篇: 3项目里面全局用less变量 cli v