通過Bundle傳遞不同Intent之間的未經處理資料

來源:互聯網
上載者:User
 
一個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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.