昊仔

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

******************************************************************************************** * 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处理。

 

  1. publicvoid startActivityForResult (Intent intent, int requestCode)  
public void startActivityForResult (Intent intent, int requestCode) 

           首先startActivityForResult 有两个参数,第一个为当前activity的intent(假设为activity A),至于第二个参数的用法,还是先看一下官方SDK文档的解释

 

 

  1. 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的标识,

  1. IntentActivity.this.startActivityForResult(intent, 15); 
IntentActivity.this.startActivityForResult(intent, 15);

同理,当在activity A的另外一个监听器要跳转到activity C时,设定activity C的标识为25。(大于或者等于0就行了)

  1. IntentActivity.this.startActivityForResult(intent, 25); 
IntentActivity.this.startActivityForResult(intent, 25);

(通过onActivityResult方法可处理代表不同activity的requestCode数值,详细用法在第3点。)

 

3、数据处理方法onActivityResult()详解

 

       然后activity A通过复写onActivityResult方法来处理这些activity返回来的数据。onActivityResult定义如下:

 

  1. protectedvoid onActivityResult (int requestCode, int resultCode, Intent data)  
protected void onActivityResult (int requestCode, int resultCode, Intent data) 

 

 

3.1、数据处理方法onActivityResult()有三个参数,第一个requestCode是用来区分哪个activity回来的数据,可以通过swith语句来筛选。

基本框架代码如下:

 

  1. switch (requestCode) { 
  2.         case15
  3.             switch (resultCode) { 
  4.             case0
  5.                 //do something  
  6.  
  7.                 break
  8.             case1
  9.                 //do something  
  10.                    
  11.                        break
  12.             default
  13.                 break
  14.             } 
  15.             break
  16.         case25
  17.             switch (resultCode) { 
  18.             case0:          
  19.                 //do something  
  20.                 break
  21.             default
  22.                 break
  23.             } 
  24.                default
  25.                         break
  26.        } 
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。当然也可以自定义为数值,自己习惯就行了。

  1. RESULT_CANCELED 
  2. RESULT_OK 
  3. RESULT_FIRST_USER 
RESULT_CANCELED
RESULT_OK
RESULT_FIRST_USER

 

具体做法如下:如你可以在activity B做如下设置,两个button的监听器对应着两个resultCode,也就对应了两种处理方式,至于你要做什么事,在监听器定义即可。

 

 

  1.               button1.setOnClickListener(new OnClickListener() { 
  2.  
  3.     @Override 
  4.     publicvoid onClick(View v) { 
  5.         // TODO Auto-generated method stub  
  6.         //do something  
  7.         secondactivity.this.setResult(0, intent); 
  8.         secondactivity.this.finish(); 
  9.  
  10.     } 
  11. }); 
  12.  
  13. button2.setOnClickListener(new OnClickListener() { 
  14.  
  15.     @Override 
  16.     publicvoid onClick(View v) { 
  17.         // TODO Auto-generated method stub  
  18.  
  19.         //do something  
  20.         secondactivity.this.setResult(1, intent); 
  21.         secondactivity.this.finish(); 
  22.     } 
  23. }); 
                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。

 

  1. switch (resultCode) { 
  2. case0
  3.     //do something  
  4.  
  5.     break
  6. case1
  7.     //do something  
  8.                 
  9.                     break
  10. default
  11.     break
			switch (resultCode) {
			case 0:
				//do something

				break;
			case 1:
				//do something
                  
                       break;
			default:
				break;
			}
			

 

===============================================================================================

附上详细代码

从第一个activity跳到第二个,主要是验证 resultCode,

从第一个activity跳到第三个activity 和跳到第二个activity,主要是验证requestCode

效果图:

  1. //IntentActivity为第一个activity  
  2.  
  3. package conowen.Activity.intent; 
  4.  
  5. import android.app.Activity; 
  6. import android.content.Intent; 
  7. import android.os.Bundle; 
  8. import android.view.View; 
  9. import android.view.View.OnClickListener; 
  10. import android.widget.Button; 
  11. import android.widget.TextView; 
  12. import android.widget.Toast; 
  13.  
  14. publicclass IntentActivity extends Activity { 
  15.  
  16.     /** Called when the Activity is first created. */ 
  17.     @Override 
  18.     publicvoid onCreate(Bundle savedInstanceState) { 
  19.         super.onCreate(savedInstanceState); 
  20.         setContentView(R.layout.main); 
  21.  
  22.         Button bt = (Button) findViewById(R.id.bt); 
  23.         Button bt2 = (Button) findViewById(R.id.bt13); 
  24.  
  25.         bt.setOnClickListener(new View.OnClickListener() { 
  26.  
  27.             @Override 
  28.             publicvoid onClick(View v) { 
  29.                 // TODO Auto-generated method stub  
  30.                 Intent intent = new Intent(IntentActivity.this
  31.                         secondactivity.class); 
  32.                 // 将intent绑定到第二个activity,然后开始跳转到第二个Activity  
  33.                 IntentActivity.this.startActivityForResult(intent, 15); 
  34.                 // 第二个参数为requestCode,大于或等于0,  
  35.  
  36.             } 
  37.         }); 
  38.  
  39.         bt2.setOnClickListener(new OnClickListener() { 
  40.  
  41.             @Override 
  42.             publicvoid onClick(View v) { 
  43.                 // TODO Auto-generated method stub  
  44.                 Intent intent = new Intent(IntentActivity.this
  45.                         requestCodetest.class);//将intent绑定在第三个activity  
  46.                 IntentActivity.this.startActivityForResult(intent, 25); 
  47.  
  48.             } 
  49.         }); 
  50.  
  51.     } 
  52.  
  53.     @Override 
  54.     protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) { 
  55.         // TODO Auto-generated method stub  
  56.         super.onActivityResult(requestCode, resultCode, data); 
  57.         switch (requestCode) {// 通过requestCode来辨别数据来自哪个activity  
  58.         case15:// 取得来自Activity2的数据,并显示于画面上  
  59.             switch (resultCode) {// 通过resultCode来辨别activity的相应处理方法  
  60.  
  61.             case0
  62.  
  63.                 String resultstr1 = data.getStringExtra("result");// 取出传回来的数据  
  64.                 TextView tv = (TextView) findViewById(R.id.tv); 
  65.                 tv.setText(resultstr1);// 显示出来  
  66.  
  67.                 break
  68.             case1
  69.  
  70.                 String resultstr2 = data.getStringExtra("result"); 
  71.                 TextView tv1 = (TextView) findViewById(R.id.tv); 
  72.                 tv1.setText(resultstr2); 
  73.                 Toast.makeText(this
  74.                         "第2个activity的requestCode的值为" + requestCode, 
  75.                         Toast.LENGTH_LONG).show(); 
  76.  
  77.                 break
  78.             default
  79.                 break
  80.             } 
  81.             break
  82.         case25
  83.             switch (resultCode) { 
  84.             case RESULT_OK: 
  85.  
  86.                 String resultstr3 = data.getStringExtra("result3"); 
  87.                 Toast.makeText(this
  88.                         "第3个activity的requestCode的值为" + requestCode, 
  89.                         Toast.LENGTH_LONG).show(); 
  90.                 TextView tv = (TextView) findViewById(R.id.tv); 
  91.                 tv.setText(resultstr3); 
  92.  
  93.                 break
  94.             default
  95.                 break
  96.             } 
  97.         default
  98.             break
  99.  
  100.         } 
  101.  
  102.     } 
//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;

		}

	}
}

 

  1. //secondactivity为第二个activity  
  2. package conowen.Activity.intent; 
  3.  
  4. import android.app.Activity; 
  5. import android.content.Intent; 
  6. import android.os.Bundle; 
  7. import android.view.View; 
  8. import android.view.View.OnClickListener; 
  9. import android.widget.Button; 
  10. import android.widget.EditText; 
  11.  
  12. publicclass secondactivity extends Activity { 
  13.  
  14.     @Override 
  15.     protectedvoid onCreate(Bundle savedInstanceState) { 
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState); 
  18.         setContentView(R.layout.secondactivity); 
  19.  
  20.         // 获取启动这个activity的intent  
  21.         Button returnbt1 = (Button) findViewById(R.id.returnbt1); 
  22.         final Intent intent = getIntent(); 
  23.         returnbt1.setOnClickListener(new OnClickListener() { 
  24.  
  25.             @Override 
  26.             publicvoid onClick(View v) { 
  27.                 // TODO Auto-generated method stub  
  28.  
  29.                 EditText et = (EditText) findViewById(R.id.et); 
  30.                 String res = et.getText().toString(); 
  31.                 intent.putExtra("result", res);// 把数据塞入intent里面  
  32.                 secondactivity.this.setResult(0, intent);// 跳转回原来的activity  
  33.                 secondactivity.this.finish();// 一定要结束当前activity  
  34.  
  35.             } 
  36.         }); 
  37.         Button returnbt2 = (Button) findViewById(R.id.returnbt2); 
  38.         returnbt2.setOnClickListener(new OnClickListener() { 
  39.  
  40.             @Override 
  41.             publicvoid onClick(View v) { 
  42.                 // TODO Auto-generated method stub  
  43.  
  44.                 intent.putExtra("result", "验证result code"); 
  45.                 secondactivity.this.setResult(1, intent); 
  46.                 secondactivity.this.finish(); 
  47.             } 
  48.         }); 
  49.  
  50.     } 
  51.  
//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();
			}
		});

	}

}
  1. //requestCodetest是第三个activity  
  2.  
  3. package conowen.Activity.intent; 
  4.  
  5. import android.app.Activity; 
  6. import android.content.Intent; 
  7. import android.os.Bundle; 
  8. import android.view.View; 
  9. import android.view.View.OnClickListener; 
  10. import android.widget.Button; 
  11. import android.widget.EditText; 
  12.  
  13. publicclass requestCodetest extends Activity { 
  14.  
  15.     @Override 
  16.     protectedvoid onCreate(Bundle savedInstanceState) { 
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState); 
  19.         setContentView(R.layout.requestcode);// layout的xml文件名不能为大写字母  
  20.  
  21.         Button bt3 = (Button) findViewById(R.id.returnbt3); 
  22.  
  23.         bt3.setOnClickListener(new OnClickListener() { 
  24.  
  25.             @Override 
  26.             publicvoid onClick(View v) { 
  27.                 // TODO Auto-generated method stub  
  28.                 // 这个activity主要是为了验证requestCode  
  29.                 EditText et3 = (EditText) findViewById(R.id.et3); 
  30.                 Intent intent = getIntent();// 获取启动这个activity的intent  
  31.                 String res = et3.getText().toString(); 
  32.                 intent.putExtra("result3", res); 
  33.                 requestCodetest.this.setResult(RESULT_OK, intent); 
  34.                 requestCodetest.this.finish(); 
  35.  
  36.             } 
  37.         }); 
  38.  
  39.     } 
  40.  
//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();

			}
		});

	}

}
posted on 2013-08-20 14:11  昊仔  阅读(246)  评论(0编辑  收藏  举报