onActivityResult传值的使用
参考地址: http://wang-peng1.iteye.com/blog/632833
http://www.cnblogs.com/linjiqin/archive/2011/06/03/2071956.html
一、如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,新的Activity 关闭后会向前面的Activity传回数据,为了得到传回的数据,必须在前面的Activity中重写onActivityResult(int requestCode, int resultCode, Intent data)方法。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private final static String TAG="MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)this.findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//得到新打开Activity关闭后返回的数据
//第二个参数为请求码,可以根据业务需求自己编号
startActivityForResult(new Intent(MainActivity.this, OtherActivity.class), 1);
}
});
}
/**
* 为了得到传回的数据,必须在前面的Activity中(指MainActivity类)重写onActivityResult方法
*
* requestCode 请求码,即调用startActivityForResult()传递过去的值
* resultCode 结果码,结果码用于标识返回数据来自哪个新Activity
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String result = data.getExtras().getString("result");//得到新Activity 关闭后返回的数据
Log.i(TAG, result);
}
}
当新Activity关闭后,新Activity返回的数据通过Intent进行传递,android平台会调用前面Activity 的onActivityResult()方法,把存放了返回数据的Intent作为第三个输入参数传入,在onActivityResult()方法中使用第三个输入参数可以取出新Activity返回的数据。
二、使用startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,新Activity关闭前需要向前面的Activity返回数据需要使用系统提供的setResult(int resultCode, Intent data)方法实现:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Button btnClose=(Button)findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//数据是使用Intent返回
Intent intent = new Intent();
//把返回数据存入Intent
intent.putExtra("result", "My name is linjiqin");
//设置返回数据
OtherActivity.this.setResult(RESULT_OK, intent);
//关闭Activity
OtherActivity.this.finish();
}
});
}
}
setResult()方法的第一个参数值可以根据业务需要自己定义,上面代码中使用到的RESULT_OK是系统Activity类定义的一个常量,值为-1,代码片断如下:
public class android.app.Activity extends ......{
public static final int RESULT_CANCELED = 0;
public static final int RESULT_OK = -1;
public static final int RESULT_FIRST_USER = 1;
}
运行结果
说明:当点击“打开新的Activity”按钮,会跳转到“我是新打开的Activity”页面;
当点击“关闭”按钮,关闭当前页面,同时跳转到“我是旧的Activity”页面,且会传递result参数给前一个Activity
请求码的作用
使用startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,我们需要为startActivityForResult()方法传入一个请求码(第二个参数)。请求码的值是根据业务需要由自已设定,用于标识请求来源。例如:一个Activity有两个按钮,点击这两个按钮都会打开同一个Activity,不管是那个按钮打开新Activity,当这个新Activity关闭后,系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法。在onActivityResult()方法如果需要知道新Activity是由那个按钮打开的,并且要做出相应的业务处理,这时可以这样做:
@Override public void onCreate(Bundle savedInstanceState) {
....
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startActivityForResult (new Intent(MainActivity.this, NewActivity.class), 1);
}
});
button2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startActivityForResult (new Intent(MainActivity.this, NewActivity.class), 2);
}
});
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case 1:
//来自按钮1的请求,作相应业务处理
case 2:
//来自按钮2的请求,作相应业务处理
}
}
}
结果码的作用
在一个Activity中,可能会使用startActivityForResult()方法打开多个不同的Activity处理不同的业务,当这些新Activity关闭后,系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法。为了知道返回的数据来自于哪个新Activity,在onActivityResult()方法中可以这样做(ResultActivity和NewActivity为要打开的新Activity):
public class ResultActivity extends Activity {
.....
ResultActivity.this.setResult(1, intent);
ResultActivity.this.finish();
}
public class NewActivity extends Activity {
......
NewActivity.this.setResult(2, intent);
NewActivity.this.finish();
}
public class MainActivity extends Activity { // 在该Activity会打开ResultActivity和NewActivity
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode){
case 1:
// ResultActivity的返回数据
case 2:
// NewActivity的返回数据
}
}
}
///////2/////////////////////////////////////////////////////////////////////////////////////
多个activity之间的传值 其实就是onActivityResult,然后别忘了还有一个action的问题 就是在主xml中添加自己的action以便于识别,最后次activity别忘了finansh。
- public class Wizard extends Activity {
- private TextView step1result, step2result, step3result;
- public static final String INTENT_STEP1 = "com.novoda.STEP1";
- public static final String INTENT_STEP2 = "com.novoda.STEP2";
- public static final String INTENT_STEP3 = "com.novoda.STEP3";
- private static final int STEP1 = 1;
- private static final int STEP2 = 2;
- private static final int STEP3 = 3;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.wizard);
- this.step1result = (TextView)findViewById(R.id.step1result);
- this.step2result = (TextView)findViewById(R.id.step2result);
- this.step3result = (TextView)findViewById(R.id.step3result);
- startActivityForResult(new Intent(Wizard.INTENT_STEP1), STEP1);
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- switch (requestCode) {
- case STEP1:
- this.step1result.setText(data.getStringExtra("STEP1RESULT"));
- startActivityForResult(new Intent(Wizard.INTENT_STEP2), STEP2);
- break;
- case STEP2:
- this.step2result.setText(data.getStringExtra("STEP2RESULT"));
- startActivityForResult(new Intent(Wizard.INTENT_STEP3), STEP3);
- break;
- case STEP3:
- this.step3result.setText(data.getStringExtra("STEP3RESULT"));
- break;
- }
- }
- }
public class Wizard extends Activity { private TextView step1result, step2result, step3result; public static final String INTENT_STEP1 = "com.novoda.STEP1"; public static final String INTENT_STEP2 = "com.novoda.STEP2"; public static final String INTENT_STEP3 = "com.novoda.STEP3"; private static final int STEP1 = 1; private static final int STEP2 = 2; private static final int STEP3 = 3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wizard); this.step1result = (TextView)findViewById(R.id.step1result); this.step2result = (TextView)findViewById(R.id.step2result); this.step3result = (TextView)findViewById(R.id.step3result); startActivityForResult(new Intent(Wizard.INTENT_STEP1), STEP1); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case STEP1: this.step1result.setText(data.getStringExtra("STEP1RESULT")); startActivityForResult(new Intent(Wizard.INTENT_STEP2), STEP2); break; case STEP2: this.step2result.setText(data.getStringExtra("STEP2RESULT")); startActivityForResult(new Intent(Wizard.INTENT_STEP3), STEP3); break; case STEP3: this.step3result.setText(data.getStringExtra("STEP3RESULT")); break; } } }
- public class Step1 extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.step1);
- Button nextStep = (Button)findViewById(R.id.goto2);
- nextStep.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- Intent it = new Intent();
- it.putExtra("STEP1RESULT", ((EditText)findViewById(R.id.step1value)).getText()
- .toString());
- setResult(Activity.RESULT_OK, it);
- finish();
- }
- });
- }
- }
public class Step1 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.step1); Button nextStep = (Button)findViewById(R.id.goto2); nextStep.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent it = new Intent(); it.putExtra("STEP1RESULT", ((EditText)findViewById(R.id.step1value)).getText() .toString()); setResult(Activity.RESULT_OK, it); finish(); } }); } }
后面的step2 step3都是一样的了
然后还有主xml
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Wizard" 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=".Step1" android:label="Step1">
- <intent-filter>
- <action android:name="com.novoda.STEP1" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity android:name=".Step2" android:label="Step2">
- <intent-filter>
- <action android:name="com.novoda.STEP2" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity android:name=".Step3" android:label="Step3">
- <intent-filter>
- <action android:name="com.novoda.STEP3" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="7" />
- </manifest>