Chinaunix首页 | 论坛 | 博客
  • 博客访问: 464388
  • 博文数量: 153
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 1724
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-08 11:55
文章分类

全部博文(153)

文章存档

2011年(1)

2010年(55)

2009年(88)

2008年(9)

我的朋友

分类: LINUX

2010-10-26 16:29:09


一个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. version="1.0" encoding="utf-8"?>  
  2.   xmlns:android="" 
  3.   package="my_pkgs_4_activity" 
  4.   android:versionCode="1" 
  5.   android:versionName="1.0.0">  
  6.   
  7.     android:icon="@drawable/icon"   
  8.     android:label="@string/app_name">  
  9.     
  10.       android:name="Activity1" 
  11.       android:label="@string/app_name">  
  12.         
  13.         android:name="android.intent.action.MAIN" />  
  14.         android:name="android.intent.category.LAUNCHER" />  
  15.         
  16.       
  17.     android:name="Activity2">  
  18.     


扩展学习:

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 等封装的方式可供使用参考。



带参数返回的情景:

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----------------------





阅读(7462) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-10-27 17:38:58

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com