Android Learning-Call Onactivityresult () to receive the returned information

Source: Internet
Author: User

1, when the other activity is called in order to get the value returned, if you choose the Address Book to enter the address book just return a phone number and so on, we can call Startactivityforresult ();

Intent intent=new Intent (); Intent.setaction ("Com.example.multiactivity.SecondActivity3"); Intent.addcategory (" Android.intent.category.DEFAULT "); Bundle B=new Bundle (); B.putstring ("Name1", "ZP"); B.putstring ("Name2", "db"); Intent.putextras (b); Startactivityforresult (Intent, 0); The //0 parameter is the request code, which is typically set to 0, which is used to identify which request returned data when multiple activity is invoked. 

2, usually in the click event, such as the ListView item, click Select Contact, return to the contact, that is, in the called Activity to save the data intent, and call Finnish return data

Intent in=new Intent () In.putextra ("Name3", "Cy"); Setresult (0, in)  ; //0 Here is the result code, the user can set a special meaning to the result code, for example, if the network operation, can be defined as the result code 200 is the return of the connection is successful, the exception occurs when the return 403 connection error finish ();

3, in the return activity to overwrite onactivityresult (int requestcode, int resultcode, Intent data) method,

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub Super.onactivityresult (Requestcode, ResultCode, data); SYSTEM.OUT.PRINTLN ("Request code:" +requestcode);   SYSTEM.OUT.PRINTLN ("Result code:" +resultcode); The result code is set for the return time if (requestcode==0)//All of the returned data operations are called when the function is completed, when the value returned by the activity is called, Processing {String st= "1" according to the request code set by Startactivityforresult , if (data!=null) St=data.getstringextra ("Name3"); Ed.settext (ST);}
}

  

For example, click "Select First Contact" When you set Requestcode to 0, click "Select Second Contact" to set the request code to 1, in Onactivityresult (int requestcode, int resultcode, Intent Data) method, according to the returned Requestcode to take the corresponding action, the relative should be in the edittext to set the phone number.

Android Learning-Call Onactivityresult () to receive the returned information

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.