******************************************************************************************** * author:conowen@大钟 * E-mail:conowen@hotmail.com * http://blog.csdn.net/conowen * 注:本文为原创,仅作为学习交流使用,转载请标明作者及出处。
********************************************************************************************/
1、两种实现activity跳转的方法
实现activity的跳转主要有两种方法,startActivity()和startActivityForResult();
例如activity A跳转到activity B
对于,第一种,是把activity A的数据,通过intent传送到activity B,但是不能返回数据。
关于第一种,可以参考我之前的这篇博文
http://blog.csdn.net/conowen/article/details/7270393
2、第二种方法startActivityForResult()的详细用法
今晚谈的是第二种方法startActivityForResult();可以实现把activity A数据传送给activity B,也可以把activity B的数据返回来给activity A处理。
- publicvoid startActivityForResult (Intent intent, int requestCode)
public void startActivityForResult (Intent intent, int requestCode)
首先startActivityForResult 有两个参数,第一个为当前activity的intent(假设为activity A),至于第二个参数的用法,还是先看一下官方SDK文档的解释
- requestCode : If >= 0, this code will be returned in onActivityResult() when the activity exits.
requestCode : If >= 0, this code will be returned in onActivityResult() when the activity exits.
一般都是设置大于或者等于0,因为activity A可能不单单要跳转到activity B,也可能跳转到activity C,D,E……,这些activity返回来的数据都交由activity处理,那activity又通过什么来分辨返回来的数据到底是哪个activity回来的呢。
所以在activity A跳转到某一个activity时,要设定目标activity的requestCode,这个requestCode就唯一地标识了相对应的activity。
如下所示,当要跳转到activity B时,15表示是activity B的标识,
- IntentActivity.this.startActivityForResult(intent, 15);
IntentActivity.this.startActivityForResult(intent, 15);
同理,当在activity A的另外一个监听器要跳转到activity C时,设定activity C的标识为25。(大于或者等于0就行了)
- IntentActivity.this.startActivityForResult(intent, 25);
IntentActivity.this.startActivityForResult(intent, 25);
(通过onActivityResult方法可处理代表不同activity的requestCode数值,详细用法在第3点。)
3、数据处理方法onActivityResult()详解
然后activity A通过复写onActivityResult方法来处理这些activity返回来的数据。onActivityResult定义如下:
- protectedvoid onActivityResult (int requestCode, int resultCode, Intent data)
protected void onActivityResult (int requestCode, int resultCode, Intent data)
3.1、数据处理方法onActivityResult()有三个参数,第一个requestCode是用来区分哪个activity回来的数据,可以通过swith语句来筛选。
基本框架代码如下:
- switch (requestCode) {
- case15:
- switch (resultCode) {
- case0:
- //do something
- break;
- case1:
- //do something
- break;
- default:
- break;
- }
- break;
- case25:
- switch (resultCode) {
- case0:
- //do something
- break;
- default:
- break;
- }
- default:
- break;
- }
switch (requestCode) { case 15: switch (resultCode) { case 0: //do something break; case 1: //do something break; default: break; } break; case 25: switch (resultCode) { case 0: //do something break; default: break; } default: break; }
3.2、第二个参数为resultCode,也是一个int类型,如果activity B有几种不同返回的结果,同样地可以通过resultCode来筛选。
当跳转到其他activity时,如activity B。需要知道的是,把activity B的数据返回去给activity A的处理方法是setResult (int resultCode, Intent data) ,可以看到它的第一个参数是resultCode,也就是对应着相应的处理方式,官方的推荐取值如下,这些是宏变量,实质是int。当然也可以自定义为数值,自己习惯就行了。
- RESULT_CANCELED
- RESULT_OK
- RESULT_FIRST_USER
RESULT_CANCELED RESULT_OK RESULT_FIRST_USER
具体做法如下:如你可以在activity B做如下设置,两个button的监听器对应着两个resultCode,也就对应了两种处理方式,至于你要做什么事,在监听器定义即可。
- button1.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- //do something
- secondactivity.this.setResult(0, intent);
- secondactivity.this.finish();
- }
- });
- button2.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- //do something
- secondactivity.this.setResult(1, intent);
- secondactivity.this.finish();
- }
- });
button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //do something secondactivity.this.setResult(0, intent); secondactivity.this.finish(); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //do something secondactivity.this.setResult(1, intent); secondactivity.this.finish(); } });
3.3、第三个参数为Intent data,为activity返回来的数值,如通过data.getStringExtra("key");就可以取出来。
注意:调用setResult()方法跳回原来的activity时,一定要调用finish方法结束当前的activity
另外,如果activity A只需要跳转到activity B而已,那跳转时,request code只要大于或者等于0就行,而数据处理的方法onActivityResult()如下结构就行了,不用筛选request code。
- switch (resultCode) {
- case0:
- //do something
- break;
- case1:
- //do something
- break;
- default:
- break;
- }
switch (resultCode) { case 0: //do something break; case 1: //do something break; default: break; }
===============================================================================================
附上详细代码
从第一个activity跳到第二个,主要是验证 resultCode,
从第一个activity跳到第三个activity 和跳到第二个activity,主要是验证requestCode
效果图:
- //IntentActivity为第一个activity
- package conowen.Activity.intent;
- 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.TextView;
- import android.widget.Toast;
- publicclass IntentActivity extends Activity {
- /** Called when the Activity is first created. */
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button bt = (Button) findViewById(R.id.bt);
- Button bt2 = (Button) findViewById(R.id.bt13);
- bt.setOnClickListener(new View.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(IntentActivity.this,
- secondactivity.class);
- // 将intent绑定到第二个activity,然后开始跳转到第二个Activity
- IntentActivity.this.startActivityForResult(intent, 15);
- // 第二个参数为requestCode,大于或等于0,
- }
- });
- bt2.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(IntentActivity.this,
- requestCodetest.class);//将intent绑定在第三个activity
- IntentActivity.this.startActivityForResult(intent, 25);
- }
- });
- }
- @Override
- protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode) {// 通过requestCode来辨别数据来自哪个activity
- case15:// 取得来自Activity2的数据,并显示于画面上
- switch (resultCode) {// 通过resultCode来辨别activity的相应处理方法
- case0:
- String resultstr1 = data.getStringExtra("result");// 取出传回来的数据
- TextView tv = (TextView) findViewById(R.id.tv);
- tv.setText(resultstr1);// 显示出来
- break;
- case1:
- String resultstr2 = data.getStringExtra("result");
- TextView tv1 = (TextView) findViewById(R.id.tv);
- tv1.setText(resultstr2);
- Toast.makeText(this,
- "第2个activity的requestCode的值为" + requestCode,
- Toast.LENGTH_LONG).show();
- break;
- default:
- break;
- }
- break;
- case25:
- switch (resultCode) {
- case RESULT_OK:
- String resultstr3 = data.getStringExtra("result3");
- Toast.makeText(this,
- "第3个activity的requestCode的值为" + requestCode,
- Toast.LENGTH_LONG).show();
- TextView tv = (TextView) findViewById(R.id.tv);
- tv.setText(resultstr3);
- break;
- default:
- break;
- }
- default:
- break;
- }
- }
- }
//IntentActivity为第一个activity package conowen.Activity.intent; 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.TextView; import android.widget.Toast; public class IntentActivity extends Activity { /** Called when the Activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button bt = (Button) findViewById(R.id.bt); Button bt2 = (Button) findViewById(R.id.bt13); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(IntentActivity.this, secondactivity.class); // 将intent绑定到第二个activity,然后开始跳转到第二个Activity IntentActivity.this.startActivityForResult(intent, 15); // 第二个参数为requestCode,大于或等于0, } }); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(IntentActivity.this, requestCodetest.class);//将intent绑定在第三个activity IntentActivity.this.startActivityForResult(intent, 25); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (requestCode) {// 通过requestCode来辨别数据来自哪个activity case 15:// 取得来自Activity2的数据,并显示于画面上 switch (resultCode) {// 通过resultCode来辨别activity的相应处理方法 case 0: String resultstr1 = data.getStringExtra("result");// 取出传回来的数据 TextView tv = (TextView) findViewById(R.id.tv); tv.setText(resultstr1);// 显示出来 break; case 1: String resultstr2 = data.getStringExtra("result"); TextView tv1 = (TextView) findViewById(R.id.tv); tv1.setText(resultstr2); Toast.makeText(this, "第2个activity的requestCode的值为" + requestCode, Toast.LENGTH_LONG).show(); break; default: break; } break; case 25: switch (resultCode) { case RESULT_OK: String resultstr3 = data.getStringExtra("result3"); Toast.makeText(this, "第3个activity的requestCode的值为" + requestCode, Toast.LENGTH_LONG).show(); TextView tv = (TextView) findViewById(R.id.tv); tv.setText(resultstr3); break; default: break; } default: break; } } }
- //secondactivity为第二个activity
- package conowen.Activity.intent;
- 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;
- publicclass secondactivity extends Activity {
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.secondactivity);
- // 获取启动这个activity的intent
- Button returnbt1 = (Button) findViewById(R.id.returnbt1);
- final Intent intent = getIntent();
- returnbt1.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- EditText et = (EditText) findViewById(R.id.et);
- String res = et.getText().toString();
- intent.putExtra("result", res);// 把数据塞入intent里面
- secondactivity.this.setResult(0, intent);// 跳转回原来的activity
- secondactivity.this.finish();// 一定要结束当前activity
- }
- });
- Button returnbt2 = (Button) findViewById(R.id.returnbt2);
- returnbt2.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- intent.putExtra("result", "验证result code");
- secondactivity.this.setResult(1, intent);
- secondactivity.this.finish();
- }
- });
- }
- }
//secondactivity为第二个activity package conowen.Activity.intent; 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; public class secondactivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); // 获取启动这个activity的intent Button returnbt1 = (Button) findViewById(R.id.returnbt1); final Intent intent = getIntent(); returnbt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EditText et = (EditText) findViewById(R.id.et); String res = et.getText().toString(); intent.putExtra("result", res);// 把数据塞入intent里面 secondactivity.this.setResult(0, intent);// 跳转回原来的activity secondactivity.this.finish();// 一定要结束当前activity } }); Button returnbt2 = (Button) findViewById(R.id.returnbt2); returnbt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub intent.putExtra("result", "验证result code"); secondactivity.this.setResult(1, intent); secondactivity.this.finish(); } }); } }
- //requestCodetest是第三个activity
- package conowen.Activity.intent;
- 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;
- publicclass requestCodetest extends Activity {
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.requestcode);// layout的xml文件名不能为大写字母
- Button bt3 = (Button) findViewById(R.id.returnbt3);
- bt3.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- // TODO Auto-generated method stub
- // 这个activity主要是为了验证requestCode
- EditText et3 = (EditText) findViewById(R.id.et3);
- Intent intent = getIntent();// 获取启动这个activity的intent
- String res = et3.getText().toString();
- intent.putExtra("result3", res);
- requestCodetest.this.setResult(RESULT_OK, intent);
- requestCodetest.this.finish();
- }
- });
- }
- }
//requestCodetest是第三个activity package conowen.Activity.intent; 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; public class requestCodetest extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.requestcode);// layout的xml文件名不能为大写字母 Button bt3 = (Button) findViewById(R.id.returnbt3); bt3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 这个activity主要是为了验证requestCode EditText et3 = (EditText) findViewById(R.id.et3); Intent intent = getIntent();// 获取启动这个activity的intent String res = et3.getText().toString(); intent.putExtra("result3", res); requestCodetest.this.setResult(RESULT_OK, intent); requestCodetest.this.finish(); } }); } }