从MainActivity打开AActivity, 在MainActivity中接收从AActivity回传 的数据"aa","bb"
一.在 MainActivity 中用startActivityForResult的打开代码
<pre name="code" class="java"> btnSend =(Button) findViewById(R.id.btnSend );
btnSend .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AActivity.class);
startActivityForResult(intent,<span style="color:#ff0000;">1</span>);
}
});
二.在 MainActivity 中的接收代码,重写onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) { //发送请求
case 1: //在发送代码中 startActivityForResult(intent,<span style="color:#ff0000;"><u>1</u></span>), 第2个参数 <span style="color:#ff0000;"><u>1</u></span>;
switch(resultCode){ //返回请求
case 3:
//OK: 来自返回请求3,作相应业务处理
Bundle bundle2 = data.getBundleExtra("bundleResult");
String data1 = bundle2.getString("data1") ;
String data2 = bundle2.getString("data2") ;
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
三.在AActivity中用setResult发送返回代码
注意,setResult 一定要在调用finish()方法前.
String data1="aa";
String data2="bb";
Intent intentA = new Intent();
intentA.setClass(AActivity.this, MainActivity.class);
Bundle bundle2 = new Bundle();
bundle2.putString("data1", data1);
bundle2.putString("data2", data2);
intentA.putExtra("bundleResult", bundle2);
setResult(<u><span style="color:#ff0000;">3</span></u>, intentA); // 3为<span style="font-family: Arial, Helvetica, sans-serif;">resultCode </span>
四.在文件AndroidManifest.xml中配置 android:launchMode="singleTop",如果是singleTask,将不能接收
<activity
android:name=".AActivity"
android:launchMode="singleTop">
</activity>