An activity needs to call another activity to transmit data at the same time. You can use android. OS. the bundle object encapsulates data and transmits the data or parameters to be passed through the bundle to transmit data between different intents. 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 );
- ......
- }
In activity1, an object is encapsulated by bundle. In activity2, encapsulated data is also unbound by bundle. in the program, data transmitted along with the bundle object is obtained using the getintent (). getextras () method. 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 ");
- ........
- }
Because there are two activities, the file must have two activity declarations. Otherwise, the system will not be able to run. See the following description.
- <? 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>
Extended learning: The bundle object provides many methods for different data types. For example, the method used to pass data of the string type in this example is bundle. putstring (stringname, stringvalue ): Bundle. putdouble ("sex", sex ); To pass data of the double type, use bundle. putdouble (doublename, doublue) as follows: Bundle. putstring ("height", height ); Otherwise, to retrieve data from the bundle object, use the corresponding methods such as bundle. getstring (stringname) and bundle. getdouble (doublename. In addition to the above simple transfer types, there are also encapsulation methods such as string [] and arraylist <string> for your reference. Scenarios returned with parameters: 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 |