Transmit original data between different intents through bundle

Source: Internet
Author: User
 
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:

  1. Public class activity1 extends Activity
  2. {
  3. @ Override
  4. Public void oncreate (bundle savedinstancestate)
  5. {
  6. Super. oncreate (savedinstancestate );
  7. ............
  8. Intent intent = new intent ();
  9. Intent. setclass (XXXX. This, XXX. Class );
  10. Bundle bundle = new bundle ();
  11. Bundle. putdouble ("height", height );
  12. Bundle. putstring ("sex", sex );
  13. Intent. putextras (bundle );
  14. Startactivity (intent );
  15. ......
  16. }

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:

  1. Public class activity2 extends Activity
  2. {
  3. @ Override
  4. Public void oncreate (bundle savedinstancestate)
  5. {
  6. Super. oncreate (savedinstancestate );
  7. ...........
  8. Bundle bunde = This. getintent (). getextras ();
  9. String sex = bunde. getstring ("sex ");
  10. Double Height = bunde. getdouble ("height ");
  11. ........
  12. }

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.

  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>

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.