| 一個Activity需要調用另外一個Activity的同時傳遞資料,可以利用android.os.Bundle對象封裝資料的能力,將欲傳遞的資料或參數,通過Bundle來傳遞不同Intent之間的資料。 Activity1:
- public class Activity1 extends Activity
- {
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- ............
- Intent intent = new Intent();
- intent.setClass(xxxx.this, xxx.class);
-
- Bundle bundle = new Bundle();
- bundle.putDouble("height",height);
- bundle.putString("sex",sex);
-
- intent.putExtras(bundle);
-
- startActivity(intent);
- ......
- }
在 Activity1是以Bundle封裝對象,自然在Activity2亦是以Bundle的方式解開封裝的資料;程式中以 getIntent().getExtras() 方法取得隨著Bundle對象傳遞過來的資料。 Activity2:
- public class Activity2 extends Activity
- {
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- ...........
- Bundle bunde = this.getIntent().getExtras();
-
-
- String sex = bunde.getString("sex");
- double height = bunde.getDouble("height");
- ........
- }
因為有兩個Activity,所以檔案中必須有兩個activity的聲明,否則系統將無法運行,請看以下的描述。
- <?xml version="1.0" encoding="utf-8"?>
- <manifest
- xmlns:android="http://schemas.android.com/apk/res/android"
- package="my_pkgs_4_activity"
- android:versionCode="1"
- android:versionName="1.0.0">
- <application
- android:icon="@drawable/icon"
- android:label="@string/app_name">
- <activity
- android:name="Activity1"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name="Activity2"></activity>
- </application>
- </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 |