前面提到建立intent 的方式的含义,这里记录下intent的作用,activity之间的值传递
当程序中需要调用一个外部功能(命令,类)来得到想要的结果时,可以用 startActivityForResult(Intent, int) 。 其中intent是想要做的事情,int 是request_code; intent中的功能执行结束时,需要返回一个
resultCode,当然也可以附加更多的参数(intent.putExtra())。
然后再在该类中重载 onActivityResult(int, int, Intent)
函数来处理返回结果。
下边是activity官方文档:
http://www.android-doc.com/reference/android/app/Activity.html
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 thestartActivityForResult(Intent, int)
version with a second integer parameter identifying the call. The result will come back through youronActivityResult(int, int, Intent)
method.
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));
}
}
}
}