androidActivity之间的跳转不只是有startActivity(Intent i)的,startActivityForResult(Intentintent, int requestCode)也是常用的方法。
其作用是可以用onActivityResult(int requestCode, int resultCode, Intent data)方法获得请求Activity结束之后的操作。
需要注意三个方法:startActivityForResult(Intent intent, int requestCode),onActivityResult(int requestCode, int resultCode, Intent data),setResult(int resultCode, Intent data)
例如如下代码:从From跳转至ToB和ToC
From:
if(条件){
Intent intent = new Intent(this, ToB.class);
startActivityForResult(serverIntent, REQUEST_CODE_01);//跳转至ToB
}else{
Intent intent = new Intent(this, ToC.class);
startActivityForResult(serverIntent, REQUEST_CODE_02);//跳转至ToC
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode) {
case REQUEST_CODE_01:
if(resultCode==Activity.RESULT_OK)
//do something
break;
case REQUEST_CODE_02:
//do something
break;
}
}
这里说说startActivityForResult(Intent intent, int requestCode)的参数,第一个Intent不用说了,需要注意的是第二个,我们用的是REQUEST_CODE_01和REQUEST_CODE_02,其实这个是我们自己定义的一个int型常量,用于标记的,具体作用可在onActivityResult方法里看到,用于判断是从哪个Activity返回的。
ToB:
Intent intent = new Intent();
intent.putExtra(key, value);
setResult(Activity.RESULT_OK, intent);
finish();//结束之后会将结果传回From
ToC:
Intent intent = new Intent();
intent.putExtra(key, value);
setResult(Activity.RESULT_OK, intent);
finish();//结束之后会将结果传回From
setResult的第一个参数对应上面onActivityResult的第二个参数,注意别把onActivityResult的第一个参数与第二个参数搞混淆了,一个是请求标记,一个是返回标记。
以下为android Developer的解释:
Starting an activity for a result
Sometimes, you might want to receive a result from the activity that you start. In that case, start the activity by calling startActivityForResult()
(instead of startActivity()). To then receive the result from the subsequent activity, implement the onActivityResult()callback method. When the subsequent activity is done, it returns a result in an Intent to your onActivityResult() method.
For example, perhaps you want the user to pick one of their contacts, so your activity can do something with the information in that contact. Here's how you can create such an intent and handle the result:
private void pickContact() { // Create an intent to "pick" a contact, as defined by the content provider URI Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // If the request went well (OK) and the request was PICK_CONTACT_REQUEST if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) { // Perform a query to the contact's content provider for the contact's name Cursor cursor = getContentResolver().query(data.getData(), new String[] {Contacts.DISPLAY_NAME}, null, null, null); if (cursor.moveToFirst()) { // True if the cursor is not empty int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); String name = cursor.getString(columnIndex); // Do something with the selected contact's name... } } }
This example shows the basic logic you should use in your onActivityResult() method in order to handle an activity result. The first condition checks whether the request was successful—if it was, then the resultCode will be RESULT_OK—and whether the request to which this result is responding is known—in this case, the requestCode matches the second parameter sent with startActivityForResult(). From there, the code handles the activity result by querying the data returned in an Intent(the data parameter).What happens is, aContentResolver performs a query against a content provider, which returns a Cursor that allows the queried data to be read. For more information, see the Content Providersdocument.
For more information about using intents, see the Intents and Intent Filters document.
两个Activity:ReceiveResult和SendResult。在ReceiveResult中实现onActivityResult(),在此方法中处理SendResult中返回的结果。
注意:
1)在ReceiveResult中定义RequestCode
2)调用startActivityForResult(),用来获取结果
3)在SendResult中用调用setResult()
具体代码如下:
ReceiveResult:
- public class ReceiveResult extends Activity {
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- if(requestCode == GET_CODE){
- Editable text = (Editable)mTextView.getText();
- if(resultCode == RESULT_CANCELED)
- text.append("Cancel");
- else{
- text.append(data.getAction());
- }
- }
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button getResult = (Button)findViewById(R.id.getButton);
- getResult.setOnClickListener(mGetResult);
- mTextView = (TextView)findViewById(R.id.results);
- mTextView.setText(mTextView.getText(), TextView.BufferType.EDITABLE);
- }
- /*
- * GET_CODE requestCode
- */
- private final int GET_CODE = 0;
- private OnClickListener mGetResult = new OnClickListener(){
- public void onClick(View v){
- Intent i = new Intent(ReceiveResult.this, SendResult.class);
- startActivityForResult(i, GET_CODE);
- }
- };
- private TextView mTextView;
- }
public class ReceiveResult extends Activity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == GET_CODE){
Editable text = (Editable)mTextView.getText();
if(resultCode == RESULT_CANCELED)
text.append("Cancel");
else{
text.append(data.getAction());
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button getResult = (Button)findViewById(R.id.getButton);
getResult.setOnClickListener(mGetResult);
mTextView = (TextView)findViewById(R.id.results);
mTextView.setText(mTextView.getText(), TextView.BufferType.EDITABLE);
}
/*
* GET_CODE requestCode
*/
private final int GET_CODE = 0;
private OnClickListener mGetResult = new OnClickListener(){
public void onClick(View v){
Intent i = new Intent(ReceiveResult.this, SendResult.class);
startActivityForResult(i, GET_CODE);
}
};
private TextView mTextView;
}
SendResult:
- public class SendResult extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sendresult);
- Button button1 = (Button)findViewById(R.id.button1);
- Button button2 = (Button)findViewById(R.id.button2);
- button1.setOnClickListener(mButton1);
- button2.setOnClickListener(mButton2);
- }
- private OnClickListener mButton1 = new OnClickListener(){
- public void onClick(View v){
- setResult(RESULT_OK, (new Intent()).setAction("Button1!"));
- finish();
- }
- };
- private OnClickListener mButton2 = new OnClickListener(){
- public void onClick(View v){
- setResult(RESULT_OK, (new Intent()).setAction("Button2!"));
- finish();
- }
- };
- }
public class SendResult extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sendresult);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(mButton1);
button2.setOnClickListener(mButton2);
}
private OnClickListener mButton1 = new OnClickListener(){
public void onClick(View v){
setResult(RESULT_OK, (new Intent()).setAction("Button1!"));
finish();
}
};
private OnClickListener mButton2 = new OnClickListener(){
public void onClick(View v){
setResult(RESULT_OK, (new Intent()).setAction("Button2!"));
finish();
}
};
}
main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_horizontal"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <TextView android:id="@+id/results"
- android:layout_width="match_parent" android:layout_height="10dip"
- android:layout_weight="1"
- android:paddingBottom="4dip"
- android:background="@drawable/green">
- </TextView>
- <Button
- android:id="@+id/getButton"
- android:text="@string/getResult"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView android:id="@+id/results"
android:layout_width="match_parent" android:layout_height="10dip"
android:layout_weight="1"
android:paddingBottom="4dip"
android:background="@drawable/green">
</TextView>
<Button
android:id="@+id/getButton"
android:text="@string/getResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
sendresult.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_horizontal"
- >
- <Button
- android:id="@+id/button1"
- android:text="@string/sendResult_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/button2"
- android:text="@string/sendResult_2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/button1"
android:text="@string/sendResult_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button2"
android:text="@string/sendResult_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>