How the message is delivered between Android development activity

Source: Internet
Author: User

<pre name= "code" class= "java" >//mainactivity.javaintent intent = new intent (); Bundle bundle = new Bundle (), Bundle.putint ("Data1", "ten"), Bundle.putstring ("Data2", "data");//From current mainactivity Switch to Activity2intent.setclass (Mainactivity.this, Activity2.class); Intent.putextras (bundle);//Start switching this.startactivity (intent);


Activity2.javaintent intent= getintent ();        Gets the value of the corresponding key in the extra of this intent object                int data1 = Intent.getextras (). GetInt ("Data1");        String data2 = Intent.getextras (). getString ("Data2");



1.startActivity Method Jump

The preceding section of code generates a intent object and a bundle object

Intent object is the parameter that the StartActivity method uses when switching

The bundle object is a key-value pair that stores the data sent to the target Activity2 object, which can not be set

Get the intent object passed to with the Getintent method in Activity2 and get the key value pair data

2.startActivityForResult Method Jump

The previous StartActivity method can pass data from mainactivity to Activity2

If you want to get the data back after the Activity2 is finished executing mainactivity

You have to use the Startactivityforresult method.

The most common requirement for this application is to identify such interfaces as pop-up boxes

               <pre code_snippet_id= "365302" snippet_file_name= "blog_20140527_6_6440008" name= "code" class= "Java" >// Mainactivity.java

Intent Intent = new Intent (); Intent.setclass (Mainactivity.this, Activity2.class);

Bundle bundle = new bundle ();

Bundle.putint ("Data1", 10);

Bundle.putstring ("Data2", "data");

Intent.putextras (bundle);

Startactivityforresult (Intent, 0);

There's only one way to change the rest, no change.

Activity2 receive key value pairs also do not need to change

Call Setresult only at the end

Activity2.javaintent intent= New Intent () Intent.putextra ("Res1", 1); Intent.putextra ("Res2", "res");// The request code can set its own setresult (RESULT_OK, intent);//Shut down the Activity2finish ();

Overriding the Onactivityresult method in Mainactivity

mainactivity.javaprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated Method Stubsuper.onactivityresult (Requestcode, ResultCode, data); switch (resultcode) {case Result_ok:string str1 = Data.getstringextra ("Res1"); String str2 = Data.getstringextra ("Res2"); Finish (); break;default:break;} Switch}//onactivityresult






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.