Android-startactivityforresult () Start activity

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. /*****************************/Gorgeous Split Line/*****************************/purpose: A.java is the main interface, B.java is a sub-function module, to start from A B, b After the work to report the results to A first look at the relevant code of A.java//--A.java--///* * To do two things, the first is to start with Startactivityforresult () b, followed by the result of recycling B *///start bintent Bintent = new Intent (a.this, b.class);//Set a value for the bintent bundle string 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 following to reclaim the bundle when identifying the//rewrite Onactivityresult () to process the returned data, it is recommended to first see B.java The code back to see here better understand//This reason has three parameters requestcode, ResultCode, dataprotected void onactivityresult (int requestcode, int resultcode, Intent data) {Switch (ResultCode) {//resultcode is the callback token, and I am returning the RESULT_OK in BCase RESULT_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 getting the data from B ... slightly */break;DefaultBreak}}-------------------------------------------------------------------------------------//--B.java--//// Setresut () Once the data is ready to be returned, just use the finish () method to send the packaged data to A and run Onactivityresult () part of the code Intent aintent = new Intent (B.this, A.class);/* The process of packaging data into aintent bundles is slightly */setresut (result_ok,aintent); This rationale has 2 parameters (int resultcode, Intent Intent) ... finish ();---------------------------------------------------------- ---------------------------OK, code as above, this time may also be a little doubt 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 () Start activity

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.