Message Transfer Between Android and Activity

Source: Internet
Author: User

When activity1 enables a new activity2, a bundle object can be bound to the intent for transmission, and many types of data can be stored in the bundle object, the supported data types include int, String, arraylist, list, Boolean, byte, and Char. If activity2 is disabled and data needs to be transmitted when activity1 is returned, activity1 must start activityforresult (intent, requestcode) activate activity2 and set setresult (result_ OK, intent) in axtivity2. When activity2 is disabled, the onactivityresult () callback function of the activity is triggered ();

The complete code is as follows:

Activity1

Code

 @ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Btnjump = (button) findviewbyid (R. Id. btnload );
Btnjump. setonclicklistener (this );
Etname = (edittext) findviewbyid (R. Id. actname );
Etpwd = (edittext) findviewbyid (R. Id. etpwd );
}

// The callback function triggered when the activity started returns the result
@ Override
Public void onactivityresult (INT requestcode, int resultcode, intent data ){
If (resultcode = result_ OK ){
Bundle bundle = data. getextras ();
Toast. maketext (this, bundle. getstring ("name") + "/" + bundle. getstring ("PWD"), Toast. length_long );
}
Super. onactivityresult (requestcode, resultcode, data );
}
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
String name = etname. gettext (). tostring ();
String Pwd = etpwd. gettext (). tostring ();
Intent intent = new intent ();
Bundle bundle = new bundle ();
Bundle. putstring ("name", name );
Bundle. putstring ("PWD", PWD );
Intent. putextras (bundle );

// Requestcode is greater than or equal to 0. When activity is returned, the onactivityresult () function is called back.
Int requestcode = 0;
// Starts an activity and the callback function is triggered when the started activity returns the result.
This. startactivityforresult (intent, requestcode );
}

 

Activity2:

 

Code

 Public class twoactivity extends activity {

Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. setcontentview (R. layout. Two );
Textview TVshow = (textview) findviewbyid (R. Id. TVshow );
Intent intent = This. getintent ();
Bundle bundle = intent. getextras ();
Final string name = bundle. getstring ("name ");
Final string Pwd = bundle. getstring ("PWD ");
TVshow. settext (name + "/" + PWD );

Button BTN = (button) findviewbyid (R. Id. btnback );
BTN. setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Intent DATA = new intent ();
Bundle extras = new bundle ();
Extras. putstring ("name", "Return:" + name );
Extras. putstring ("PWD", "Return:" + PWD );
Data. putextras (extras );
Twoactivity. This. setresult (result_ OK, data); // return data
Finish ();
}
});
}
}

 

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.