Android頁面之間進行數(shù)據(jù)回傳的方法分析
本文實例講述了Android頁面之間進行數(shù)據(jù)回傳的方法。分享給大家供大家參考,具體如下:
要求:頁面1跳轉(zhuǎn)到頁面2,頁面2再返回頁面1同時返回數(shù)據(jù)
頁面1添加如下代碼:
Intent intent = new Intent();
intent.setClass(頁面1.this, 頁面2.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);//將Bundle添加到Intent,也可以在Bundle中添加相應數(shù)據(jù)傳遞給下個頁面,例如:bundle.putString("abc", "bbb");
startActivityForResult(intent, 0);// 跳轉(zhuǎn)并要求返回值,0代表請求值(可以隨便寫)
頁面2接收數(shù)據(jù)添加代碼如下:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
bundle.putString("aaa", "back");//添加要返回給頁面1的數(shù)據(jù)
intent.putExtras(bundle);
this.setResult(Activity.RESULT_OK, intent);//返回頁面1
this.finish();
頁面1接收返回數(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();
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android線程與消息機制用法總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android開發(fā)入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。
- Android基礎(chǔ)之使用Fragment控制切換多個頁面
- android使用webwiew載入頁面使用示例(Hybrid App開發(fā))
- Android使用Fragment打造萬能頁面切換框架
- Android編程實現(xiàn)ViewPager多頁面滑動切換及動畫效果的方法
- 詳解Android Activity之間切換傳遞數(shù)據(jù)的方法
- Android開發(fā)之利用Intent實現(xiàn)數(shù)據(jù)傳遞的方法
- Android中Service實時向Activity傳遞數(shù)據(jù)實例分析
- Android實現(xiàn)Activities之間進行數(shù)據(jù)傳遞的方法
- Android學習筆記--通過Application傳遞數(shù)據(jù)代碼示例
- 在Android系統(tǒng)中使用gzip進行數(shù)據(jù)傳遞實例代碼
- Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對象的應用
- Android 使用Intent傳遞數(shù)據(jù)的實現(xiàn)思路與代碼

