[Reprint] Use bundle to transmit messages between Android activities

Source: Internet
Author: User

ProgramThe effect is that activity a redirects to B and sends a string while Activity B reads the string.

Define a new activity in androidmanifest. XML, target:

XML Code
  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Manifest Xmlns: Android=Http://schemas.android.com/apk/res/android"
  3. Package="Com. Ray. test"
  4. Android: versioncode="1"
  5. Android: versionname="1.0">
  6. <Application Android: icon="@ Drawable/icon" Android: Label="@ String/app_name">
  7. <Activity Android: Name=". Testbundle"
  8. Android: Label="@ String/app_name">
  9. <Intent-Filter>
  10. <Action Android: Name="Android. Intent. Action. Main" />
  11. <Category Android: Name="Android. Intent. Category. launcher" />
  12. </Intent-Filter>
  13. </Activity>
  14. <Activity Android: Name=". Target"></Activity>
  15. </Application>
  16. <Uses-SDK Android: minsdkversion="3" />
  17. manifest >
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. ray. test "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <activity Android: Name = ". testbundle "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 = ". target "> </activity> </Application> <uses-SDK Android: minsdkversion =" 3 "/> </manifest>

 

The code for the first activity is as follows:

Java code
  1. PackageCom. Ray. test;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. content. intent;
  4. ImportAndroid. OS. Bundle;
  5. ImportAndroid. View. motionevent;
  6. Public ClassTestbundleExtendsActivity {
  7. Public VoidOncreate (bundle savedinstancestate ){
  8. Super. Oncreate (savedinstancestate );
  9. Setcontentview (R. layout. Main );
  10. }
  11. Public BooleanOntouchevent (motionevent event ){
  12. Intent intent =NewIntent ();
  13. Intent. setclass (testbundle.This, Target.Class);
  14. Bundle mbundle =NewBundle ();
  15. Mbundle. putstring ("Data","Ray 'blog");// Press data
  16. Intent. putextras (mbundle );
  17. Startactivity (intent );
  18. Finish ();
  19. Return Super. Ontouchevent (event );
  20. }
  21. }
Package COM. ray. test; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. motionevent; public class testbundle extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main);} public Boolean ontouchevent (motionevent event) {intent = new intent (); intent. setclass (testbundle. this, target. class); bundle mbundle = new bundle (); mbundle. putstring ("data", "Ray 'blog"); // press the data intent. putextras (mbundle); startactivity (intent); finish (); return Super. ontouchevent (event );}}

Activity Code to jump:

Java code
  1. PackageCom. Ray. test;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. OS. Bundle;
  4. Public ClassTargetExtendsActivity {
  5. Public VoidOncreate (bundle savedinstancestate ){
  6. Super. Oncreate (savedinstancestate );
  7. Setcontentview (R. layout. Main );
  8. Bundle bundle = getintent (). getextras ();
  9. String data = bundle. getstring ("Data");// Read data
  10. Settitle (data );
  11. }
  12. }

If you want to return data/information from target to testbundle, you can:
Startactivityforresult (intent, 1333); // 1333 is the Request Code

Then in the target, when the return is:
Intent DATA = new data ();
Data. putextra ("MSG", "My message ");
Setresult (result_ OK, data );
Finish ();

Data/information returned from testbundle:
@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
String MSG = data. getextras. getstring ("MSG ");
//...
}

 

 

 

Related Article

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.