Android uses bundle to transmit messages between activities

Source: Internet
Author: User

Message transmission between activities. Bundle is a convenient method.

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

<? 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" type = "codeph" text = "/codeph">

<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>

<? 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" type = "codeph" text = "/codeph">

<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

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 intent = new intent ();

Intent. setclass (testbundle. This, target. Class );

Bundle mbundle = new bundle ();

Mbundle. putstring ("data", "Ray 'blog"); // press data

Intent. putextras (mbundle );

Startactivity (intent );

Finish ();

Return super. ontouchevent (event );

}
Women's brand list
}

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);

}< br>
Public Boolean ontouchevent (motionevent event) {
intent = new intent ();
intent. setclass (testbundle. this, target. class);
bundle mbundle = new bundle ();
mbundle. putstring ("data", "Ray 'blog"); // press data
intent. putextras (mbundle);
startactivity (intent);
finish ();
return Super. ontouchevent (event);

}

activity code to jump to:
JAVA code
package COM. ray. test;
freight transport expert
Import android. app. activity;
Import android. OS. bundle;
public class target extends activity {
Public void oncreate (bundle savedinstancestate) {
super. oncreate (savedinstancestate);
setcontentview (R. layout. main);
bundle = getintent (). getextras ();
string data = bundle. getstring ("data"); // read data
settitle (data);

}

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.