1.page_1
public void toPage2(View v){
Intent intent = new Intent(this,page_2.class);//設定傳送參數
Bundle bundle = new Bundle();
bundle.putString("key1", "value1");
intent.putExtras(bundle); //將參數放入
intent startActivityForResult(intent, 0); //呼叫page2並要求回傳值
}
2.page_2接收參數
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();//取得Bundle
TextView txt_1 = (TextView)findViewById(R.id.txt1);
txt_1.setText(bundle.getString("name1")); //輸出Bundle內容
3.若要回傳值
bundle.putString("key2", "value2");
intent.putExtras(bundle); //將key2放入Bundle
page_2.this.setResult(Activity.RESULT_OK, intent); //回傳RESULT_OK
page_2.this.finish(); //關閉Activity
4.回到page_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();
TextView txt_2 = (TextView)findViewById(R.id.txt2);
txt_2.setText(bundle.getString("key2"));
}}
留言
張貼留言