在原文档中是:Start Activity and Getting Results
The startActivity(android.content.Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed. Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) 用startActivityForResult(Intent, int) 方法替代了原来的startActivity方法。 version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, android.content.Intent) method. When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), 在主Activity中重写onActivity方法来得到返回的结果! along with the integer identifier it originally supplied. If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED. public class MyActivity extends Activity { ... static final int PICK_CONTACT_REQUEST = 0; protected boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { // When the user center presses, let them pick a contact. startActivityForResult( new Intent(Intent.ACTION_PICK, new Uri("content://contacts")), PICK_CONTACT_REQUEST); return true; } return false; } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICK_CONTACT_REQUEST) { if (resultCode == RESULT_OK) { // A contact was picked. Here we will just display it // to the user. startActivity(new Intent(Intent.ACTION_VIEW, data)); } } } }
在主activity中
重点在于用startActivityForResult(Intent, requestCode) 方法替代了原来的startActivity方法。
且多加了:onActivityResult(int requestCode, int resultCode, Intent data) 方法得到子activity返回的结果
自己的程序:
1 package com.example.saagr; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.view.Menu; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.EditText; 11 import android.widget.TextView; 12 13 public class MainActivity extends Activity { 14 private EditText editText, editText2, editText3; 15 private Button button; 16 private TextView textView, textView2; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 button = (Button) findViewById(R.id.but_post); 23 editText = (EditText) findViewById(R.id.et1); 24 editText2 = (EditText) findViewById(R.id.et2); 25 editText3 = (EditText) findViewById(R.id.et3); 26 textView = (TextView) findViewById(R.id.tv_add); 27 textView2 = (TextView) findViewById(R.id.tv_equ); 28 button.setOnClickListener(new OnClickListener() { 29 30 @Override 31 public void onClick(View v) { 32 // TODO Auto-generated method stub 33 String s1 = editText.getText().toString(); 34 String s2 = editText2.getText().toString(); 35 Intent intent = new Intent(MainActivity.this, SubActivity.class); 36 intent.putExtra("message", s1 + "+" + s2 + "=" + "?"); 37 startActivityForResult(intent, 1000); 38 39 } 40 }); 41 42 } 43 44 @Override 45 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 46 // TODO Auto-generated method stub 47 super.onActivityResult(requestCode, resultCode, data); 48 if (requestCode == 1000 && resultCode == 1001) { 49 String result = data.getStringExtra("result"); 50 editText3.setText(result); 51 } 52 } 53 54 @Override 55 public boolean onCreateOptionsMenu(Menu menu) { 56 // Inflate the menu; this adds items to the action bar if it is present. 57 getMenuInflater().inflate(R.menu.main, menu); 58 return true; 59 } 60 61 }
在子activity中
用setResult(int resultCode, Intent data)方法给主activity返回结果
自己的程序:
package com.example.saagr; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class SubActivity extends Activity { private TextView textView; private EditText editText; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); textView = (TextView) findViewById(R.id.tv_show); editText = (EditText) findViewById(R.id.et); button = (Button) findViewById(R.id.but); Intent intent = getIntent(); String message = intent.getStringExtra("message"); textView.setText(message); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String result = editText.getText().toString(); Intent intent = new Intent(); intent.putExtra("result", result); setResult(1001, intent); finish(); } }); } }
总结:
主activity用startActivityForResult(Intent, requestCode) 方法开启子activity并且会传过去一个requestCode用来标识;
子activity用getIntent()得到发过来的数据并进行处理,再将结果数据封装成一个intent用setResult(int resultCode, Intent intent)返回给主activity(resultCode也是用来标识的);
主activity再用onActivityResult(int requestCode, int resultCode, Intent data) 方法得到返回的结果并且在此方法体中会定义判断语句来判断requestCode和resultCode是否和相应的标识相符,再对结果进行处理