通过Bundle传递不同Intent之间的原始数据

 

一个Activity需要调用另外一个Activity的同时传递数据,可以利用android.os.Bundle对象封装数据的能力,将欲传递的数据或参数,通过Bundle来传递不同Intent之间的数据。

Activity1:

  1. public class Activity1 extends Activity   
  2. {  
  3.    
  4.   @Override 
  5.   public void onCreate(Bundle savedInstanceState)   
  6.   {  
  7.     super.onCreate(savedInstanceState); 
  8.        ............
  9.         Intent intent = new Intent();  
  10.         intent.setClass(xxxx.this, xxx.class);  
  11.           
  12.         Bundle bundle = new Bundle();  
  13.         bundle.putDouble("height",height);  
  14.         bundle.putString("sex",sex);  
  15.           
  16.         intent.putExtras(bundle);  
  17.                  
  18.         startActivity(intent);
  19.      ......
  20. }
在 Activity1是以Bundle封装对象,自然在Activity2亦是以Bundle的方式解开封装的数据;程序中以 getIntent().getExtras() 方法取得随着Bundle对象传递过来的数据。

Activity2:
  1. public class Activity2 extends Activity   
  2. {  
  3.    
  4.   @Override 
  5.   public void onCreate(Bundle savedInstanceState)  
  6.   {  
  7.     super.onCreate(savedInstanceState); 
  8. ...........
  9. Bundle bunde this.getIntent().getExtras();  
  10.       
  11.      
  12.     String sex = bunde.getString("sex");  
  13.     double height = bunde.getDouble("height");
  14. ........
  15. }

因为有两个Activity,所以文件中必须有两个activity的声明,否则系统将无法运行,请看以下的描述。

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

扩展学习:

Bundle对象针对了不同的数据类型提供了许多的方法,例如,此范例中传递String类型的数据,使用的方法为 Bundle.putString(stringName,stringValue):

       bundle.putDouble("sex",sex); 

而要传递Double类型的数据,使用的方法为Bundle.putDouble(doubleName,doublue),如下:

       bundle.putString("height",height); 

反之,若要由Bundle对象中取出数据,则使用Bundle.getString(stringName)、 Bundle.getDouble(doubleName) 等相对应的方法即可。

除了上述简单的传递类型之外,尚有String[] 与ArrayList<String> 等封装的方式可供使用参考。



带参数返回的情景:

Activity1:

Bundle newExtras = new Bundle();
            if (cropValue.equals("circle")) {
                newExtras.putString("circleCrop", "true");
            }

            Intent cropIntent = new Intent();
            cropIntent.setData(img.fullSizeImageUri());
            cropIntent.setClass(this, CropImage.class);
            cropIntent.putExtras(newExtras);

            /* pass through any extras that were passed in */
            cropIntent.putExtras(myExtras);
            startActivityForResult(cropIntent, CROP_MSG);

 

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        switch (requestCode) {
            case MenuHelper.RESULT_COMMON_MENU_CROP: {
                if (resultCode == RESULT_OK) {
                   mCropResultUri = Uri.parse(data.getAction());
                }
                break;
            }
            case CROP_MSG: {
                if (resultCode == RESULT_OK) {
                    setResult(resultCode, data);
                    finish();
                }
                break;
            }
        }
    }

 

Activity2:

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();

        if (extras != null) {
            if (extras.getString("circleCrop") != null) {
                mCircleCrop = true;
                mAspectX = 1;
                mAspectY = 1;
            }
           mSaveUri = (Uri)extras.getParcelable(MediaStore.EXTRA_OUTPUT);

............

        Bundle myExtras = getIntent().getExtras();
        if (myExtras != null && (myExtras.getParcelable("data") != null
                || myExtras.getBoolean("return-data"))) {
            Bundle extras = new Bundle();
            extras.putParcelable("data", croppedImage);
            setResult(RESULT_OK,
                    (new Intent()).setAction("inline-data").putExtras(extras));
            finish();

 

-------------EOF----------------------

http://blog.chinaunix.net/u2/87831/showart_2369989.html


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值