android数据回传多个页面_Android页面之间进行数据回传
要求:頁(yè)面1跳轉(zhuǎn)到頁(yè)面2,頁(yè)面2再返回頁(yè)面1同時(shí)返回?cái)?shù)據(jù)
頁(yè)面1添加如下代碼:
Intent intent = new Intent();
intent.setClass(頁(yè)面1.this, 頁(yè)面2.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);//將Bundle添加到Intent,也可以在Bundle中添加相應(yīng)數(shù)據(jù)傳遞給下個(gè)頁(yè)面,例如:bundle.putString("abc", "bbb");
startActivityForResult(intent, 0);// 跳轉(zhuǎn)并要求返回值,0代表請(qǐng)求值(可以隨便寫)
頁(yè)面2接收數(shù)據(jù)添加代碼如下:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
bundle.putString("aaa", "back");//添加要返回給頁(yè)面1的數(shù)據(jù)
intent.putExtras(bundle);
this.setResult(Activity.RESULT_OK, intent);//返回頁(yè)面1
this.finish();
頁(yè)面1接收返回?cái)?shù)據(jù):(需要重寫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();
}
}
總結(jié)
以上是生活随笔為你收集整理的android数据回传多个页面_Android页面之间进行数据回传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 相机成像原理_【亲子科学小实验】相机原理
- 下一篇: android线程间通信的几种方法_An