Android Startactivityforresult ()

Source: Internet
Author: User

For example, one of the things I want to do is to be able to connect to many different sub-function modules (sub-activity) on a main interface (the main activity), when the submodule is done and back to the main interface, perhaps returning some of the Submodule's completed data to the main activity handler.

Purpose: A.java is the main interface, B.java is a sub-function module, to start from a b,b after the finished work to report the results to a first look at A.java related code//--a.java--///* * To do two things, the first is to use Startactivityforresult () Start B, followed by the result of recycling B */
//Start BIntent bintent =NewIntent (A. ThisB.class);//set a value for the bundle of BintentString Bsay ="Hello, this is B speaking"; Bintent.putextra ("Listenb", Bsay) Startactivityforresult (bintent,0);//the Requestcode of the parameter (Intent Intent, Int requestcode) corresponds to the identification used when the bundle is recycled below//rewrite Onactivityresult () to process the returned data, it is recommended to look at the B.java code before you come back to see it better understand//This has three parameters Requestcode, ResultCode, dataprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {Switch(ResultCode) {//ResultCode is the sign of the callback, and I'm RESULT_OK in B . CaseResult_ok:bundle b=data.getextras ();//data is the intent of the callbacks in BString str=b.getstring ("Listenb");//STR is the value of the callback "Hello, this is B speaking"/*What to do after I get the data from B ... Slightly*/                       Break;default:           Break;}}
--B.java--////with Setresut () ready to return the data, as long as the use of the finished () method can be packaged data sent to a and run Onactivityresult () part of the Code
New Intent (B.This, A.class); /*  */ // This reason has 2 parameters (int resultcode, Intent Intent)... finish ();
OK, the code as above, may be a bit doubtful at this time, about the parameters of the question. Looking directly at the Android SDK helps to speak more clearly. I find there are some articles on the internet and Requestcode and ResultCode confused the wrong explanation. Startactivityforresult (Intent Intent, Int requestcode) Intent passed to B, do not explain, do not understand you or play mobile phone, do not want to develop the things requestcode >=0, casually used in Onactivityresult () to distinguish which sub-module callback data, if there is C.java, D even e sub-modules, each district separate different requestcode is good. setresut (int resultcode, Intent Intent) ResultCode if the B sub-module may have several different results returned, it can be distinguished by this parameter. There is also a special RESULT_OK value, there is no special case to use it, the SDK is explained, uh. Intent continued not to explain, passed back to a of the Onactivityresult () onactivityresult (int requestcode, int resultcode, Intent Intent)Here are three not to explain, and the above corresponding to the thing. If Requestcode and ResultCode are not differentiated, any other activity setresult to a onactivityresult () will be treated with no distinction.

Android Startactivityforresult ()

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.