//MainActivity
package com.bwei.day_05_startactivityforresult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewActivity.class);
intent.putExtra("name", "张三");
startActivityForResult(intent, 110);
// finish();
}
@Override
// 参数一:请求码,参数二:结果码;参数三:回传的数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
String str_data = data.getStringExtra("new");
System.out.println("requestCode" + requestCode + " resultCode"
+ resultCode + " str_data: " + str_data);
}
}
//NewActivity
package com.bwei.day_05_startactivityforresult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class NewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
Intent intent = getIntent();
String str_name = intent.getStringExtra("name");
System.out.println("前面activity传过来的值:" + str_name);
// ///
// 回传给上一个Activity的数据
Intent data = new Intent();
data.putExtra("new", "newActivity回传回来的数据");
setResult(120, data);
}
}