Android Apidemos Sample Resolution (one) App->activity->receive result

Source: Internet
Author: User

StartActivity can start a new activity, which is relatively independent of the activity that initiates it, and does not return to the result of the activity that invoked it. When writing applications, it is sometimes necessary to return results from other activity. Receive result shows this usage.

Startactivityforresult use methods similar to startactivity, it is important that Startactivityforresult can return results from the invoked activity. Get_code is the request code, which can be used to handle the results returned by the activity according to the request code when the child activity returns the result.

Definition of the one requestcode we use for receiving resuls.     
Static final private int get_code = 0;     

Private Onclicklistener Mgetlistener = new Onclicklistener () {public     
    void OnClick (View v.) {     
        //Start the activity whose result we want to retrieve.  The     
        //result would come back with request code Get_code.     
        Intent Intent = new Intent (receiveresult.this, sendresult.class);     
        Startactivityforresult (Intent, Get_code);     
    }     
;

When the child activity ends, call Setresult to set the return result. The Receive result example involves two activity, Receiveresult for the parent Activity,sendresult returns the user option for the child activity, and the results returned are displayed in Receiveresult.

Sendresult returns the following code for the result:

Private Onclicklistener Mcorkylistener = new Onclicklistener ()     
{public     
    void OnClick (View v)     
    {     
        //To Send a result, simply call Setresult () before your     
        //the activity is finished.     
        Setresult (RESULT_OK, (New Intent ()). Setaction ("corky!"));     
        Finish ();     
    }     
;     

Private Onclicklistener Mvioletlistener = new Onclicklistener ()     
{public     
    void OnClick (View v)     
    {     
        // To send a result, the Simply call Setresult () before your//the activity is     
        finished.     
        Setresult (RESULT_OK, (New Intent ()). Setaction ("violet!"));     
        Finish ();     
    }     
;

When the result is returned from a child activity, the parent activity processes the results returned by the child activity by defining Onactivityresult.

protected void Onactivityresult (int requestcode, int resultcode,     
        Intent data) {     
        //can use the Requestcode to Select between multiple child     
        //Activities your may have started.  Here there are only one thing     
        //We launch.     
        if (Requestcode = = Get_code) {     

            //We'll be adding to our text.     
           ...     
        }     
    }

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.