startActivityForResult的使用和用法
startActivityForResult 和 onActivityResult在activity间传递数据AndroidManifest.xml<applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name" ><activityandroid:name=".KakuLogerActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".LaunchActivity" android:label="@string/app_name2" ></activity></application>主Activity: public class KakuLogerActivity extends Activity {TextView phoneNumber;EditText iNameField;int REQUEST_CODE = 0;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button launchBtn = (Button)findViewById(R.id.btnLaunchActivity);iNameField = (EditText)findViewById(R.id.inamefield);phoneNumber = (TextView)findViewById(R.id.phone_number);launchBtn.setOnClickListener(new OnClickListener(){@Override public void onClick(View v) { Intent newIntent = new Intent(KakuLogerActivity.this, LaunchActivity.class);// 传递参数newIntent.putExtra("name", iNameField.getText().toString()); // 开始一个新的 Activity等候返回结果 startActivityForResult(newIntent, REQUEST_CODE);Log.i("clicked", "open the LaunchActivity page!!!!");}});}@Override// 当结果返回后判断并执行操作protected void onActivityResult(int requestCode, int resultCode, Intent intent) {super.onActivityResult(requestCode, resultCode, intent);if (requestCode == REQUEST_CODE) {if (resultCode == RESULT_OK) {Bundle extras = intent.getExtras();if (extras != null) {phoneNumber.setText("Phone #: "+ extras.getString("phonenumber"));}}}} } LaunchActivity: public class LaunchActivity extends Activity {EditText phoneNumber;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout2);Button btn = (Button)findViewById(R.id.btnRtrnActivity);TextView oNameField = (TextView)findViewById(R.id.oNameMessege);phoneNumber = (EditText)findViewById(R.id.iTelephoneField);Log.i("launchActivity", "opened the LaunchActivity page!!!!");// 取得前一个画面的传入值Bundle extras = getIntent().getExtras();oNameField.setText("Hello " + extras.getString("name"));btn.setOnClickListener(new OnClickListener(){ public void onClick(View v) {// 设置返回数据Bundle bundle = new Bundle();bundle.putString("phonenumber", phoneNumber.getText().toString());Intent intent = new Intent();intent.putExtras(bundle);// 返回intent setResult(RESULT_OK, intent);finish();}});} }