startActivityForResult()方法

在启动另外一个Activity的时候,有两种方法,一种是直接使用startActivity,另外一种就是使用startActivityForResult。前一种想必大家都明白怎么使用了,我就不废话了。本文主要通过一个Demo来学习一下第二种。

startActivityForResult的主要作用就是它可以回传数据,假设我们有两个页面,首先进入第一个页面,里面有一个按钮,用于进入下一个页面,当进入下一个页面时,进行设置操作,并在其finish()动作或者back动作后,将设置的值回传给第一个页面,从而第一个页面来显示所得到的值。这个有一点像回调方法,就是在第二个页面finish()动作或者back动作后,会回调第一个页面的onActivityResult()方法,所以我们可以重写一下这个方法。直接看代码吧:
第一个页面代码:

  1. package org.sunchao;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.   
  11. public class TestStartActivityForResultActivity extends Activity implements  
  12.         OnClickListener {  
  13.     private TextView mText01;  
  14.     private TextView mText02;  
  15.     private Button button01;  
  16.     private Button button02;  
  17.     private Intent mIntent;  
  18.     private int requestCode;  
  19.   
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         mText01 = (TextView) findViewById(R.id.mText01);  
  26.         mText02 = (TextView) findViewById(R.id.mText02);  
  27.         button01 = (Button) findViewById(R.id.mButton01);  
  28.         button02 = (Button) findViewById(R.id.mButton02);  
  29.         button01.setOnClickListener(this);  
  30.         button02.setOnClickListener(this);  
  31.         mText01.setText("01");  
  32.         mText02.setText("02");  
  33.   
  34.         mIntent = new Intent();  
  35.         mIntent.setClass(TestStartActivityForResultActivity.this,  
  36.                 Activity02.class);  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onClick(View v) {  
  41.         switch (v.getId()) {  
  42.         case R.id.mButton01:  
  43.             // 请求码的值随便设置,但必须>=0  
  44.             requestCode = 0;  
  45.             startActivityForResult(mIntent, requestCode);  
  46.             break;  
  47.         case R.id.mButton02:  
  48.             requestCode = 2;  
  49.             startActivityForResult(mIntent, requestCode);  
  50.             break;  
  51.         default:  
  52.             break;  
  53.         }  
  54.     }  
  55.   
  56.     // 回调方法,从第二个页面回来的时候会执行这个方法  
  57.     @Override  
  58.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  59.         String change01 = data.getStringExtra("change01");  
  60.         String change02 = data.getStringExtra("change02");  
  61.         // 根据上面发送过去的请求吗来区别  
  62.         switch (requestCode) {  
  63.         case 0:  
  64.             mText01.setText(change01);  
  65.             break;  
  66.         case 2:  
  67.             mText02.setText(change02);  
  68.             break;  
  69.         default:  
  70.             break;  
  71.         }  
  72.     }  
  73. }  

第一个页面布局文件: 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:id="@+id/mText01"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     />  
  12. <TextView    
  13.     android:id="@+id/mText02"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     />  
  17. <Button   
  18.     android:id="@+id/mButton01"  
  19.     android:layout_width="fill_parent"   
  20.     android:layout_height="wrap_content"  
  21.     android:text="改变第一行文本的值"  
  22.     />  
  23. <Button   
  24.     android:id="@+id/mButton02"  
  25.     android:layout_width="fill_parent"   
  26.     android:layout_height="wrap_content"  
  27.     android:text="改变第二行文本的值"  
  28.     />  
  29. </LinearLayout>  

第二个页面代码: 

  1. package org.sunchao;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6.   
  7. public class Activity02 extends Activity {  
  8.     private int resultCode = 0;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity02);  
  14.         Intent mIntent = new Intent();  
  15.         mIntent.putExtra("change01""1000");  
  16.         mIntent.putExtra("change02""2000");  
  17.         // 设置结果,并进行传送  
  18.         this.setResult(resultCode, mIntent);  
  19.         // this.finish();  
  20.     }  
  21.   
  22. }  

第二个页面布局文件: 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="文本的值已经改变"  
  11.     />  
  12. </LinearLayout>  

AndroidManifest.xml: 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="org.sunchao"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".TestStartActivityForResultActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.         <activity android:name=".Activity02" />  
  17.   
  18.     </application>  
  19. </manifest>  

运行效果图: 
 
 


 

代码下载地址: http://download.csdn.net/source/3448804 

 

假设:我这里有两个Activity:A和B,从A中向B中传递数据的时候采用的是Bundle封装数据,然后从A中跳转到B中,当B有需求将数据封装起来回传给A并跳转回A。那么A中接收数据时还要先判断Bundle是否为空,因为第一次访问A的时候(即B还没有回传的时候),Bundle是为空的,这样显然是比较麻烦的,不明智的做法。
还好startActivityForResult来做跳转给了我们更好的解决办法。
1.跳转的时候不是采用startActivity(intent) 这个方法,而是startActivityForResult(intent, 0)。
Intent intent=new Intent();
intent.setClass(A.this, B.class);
Bundle bundle=new Bundle();
String str1="aaaaaa";
bundle.putString("str1", str1);
intent.putExtras(bundle);
startActivityForResult(intent, 0);//这里采用startActivityForResult来做跳转,此处的0为一个依据,可以写其他的值,但一定要>=0
2.重写onActivityResult方法,用来接收B回传的数据。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) { //resultCode为回传的标记,我在B中回传的是RESULT_OK
   case RESULT_OK:
Bundle b=data.getExtras(); //data为B中回传的Intent
   String str=b.getString("str1");//str即为回传的值
break;
default:
break;
}
}
3.在B中回传数据时采用setResult方法,并且之后要调用finish方法。
setResult(RESULT_OK, intent); //intent为A传来的带有Bundle的intent,当然也可以自己定义新的Bundle
finish();//此处一定要调用finish()方法
这样当B中调用finish方法的时候,跳转到A时会自动调用onActivityResult方法,来获取B中回传的intent了。
posted @ 2014-04-12 14:06  Mentos  阅读(232)  评论(0编辑  收藏  举报