Android--Startactivityforresult-------&&&----setresult

Source: Internet
Author: User

the difference between Startactivityforresult and startactivity
StartActivity ()

Just jump to the target page, and if you want to jump back to the current page, you must use StartActivity () once more.

Startactivityforresult ()

This task can be done one time, when the program executes to this code, if from T1activity jump to the next text2activity, and when the text2activity call the Finish () method, the program will automatically jump back t1activity , and call the Onactivityresult () method in the previous t1activity.

Code

Jump not using startactivity (intent) This method, but Startactivityforresult (intent, 0)

Intent intent=new Intent (); Intent.setclass (A.this, B.class); Bundle Bundle=new Bundle (); String str1= "aaaaaa"; bundle.putstring ("str1", str1); Intent.putextras (bundle); Startactivityforresult (intent, 0);// Here use Startactivityforresult to do jump, here 0 is a basis, can write other values, but must >=0

Override the Onactivityresult method to receive data from B callbacks.

protected void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (ResultCode) {//resultcode is a callback token, I The callback in B is RESULT_OK case   RESULT_OK:    Bundle B=data.getextras ();//data is the intent String str=b.getstring for callbacks in B    ( "str1");//str is the value of the callback    Break;default: Break    ;    }}

The Setresult method is used when the data is returned in B, and then the finish method is called.

Setresult (RESULT_OK, intent); Intent is a bundle of intent, of course, you can also define a new bundlefinish ();//You must call the Finish () method here
Setresult ()

If Setresult is set in the Startactivityforresult activity, the result is not immediately returned to the parent's activity, only the current activity is finish, The result will be sent to the parent's onactivityresult to deal with!

Public final void Setresult (Int. ResultCode, Intent data) {        synchronized (this) {            mresultcode = ResultCode;            Mresultdata = data;        }    }    public void Finish () {        if (mparent = = null) {            int resultcode;            Intent Resultdata;            Synchronized (this) {                resultcode = Mresultcode;                Resultdata = Mresultdata;            }            if (CONFIG.LOGV) log.v (TAG, "finishing self:token=" + Mtoken);            try {                if (Activitymanagernative.getdefault ()                    . Finishactivity (Mtoken, ResultCode, Resultdata)) {                    Mfinished = true;                }            } catch (RemoteException e) {                //Empty            }        }} else {            mparent.finishfromchild (this);}    }

This code shows that the activity returns result at the time of the finish, which means that the call to the Setresult () method must precede the finish (). If you call Setresult () in the following method, it may not return success: OnPause (), OnStop (), OnDestroy (),
Because these method calls are not necessarily before the finish, of course OnCreate () on the call Setresult must be before finish

Press the back key to exit from an activity, a press back,android will automatically invoke the activity's finish () method, and then set ResultCode to Result_canceled, will not return any data.
The solution is to capture the events in the activity by the back, capture the first Setresult, and then call finish, then it's done ... To swallow the back incident directly.

@Override public    void onbackpressed () {        log.i (TAG, "onbackpressed");        Setresult (CONST.LIVE_OK);        Super.onbackpressed ();    }
I'm the dividing line of the king of the Land Tiger.

Reference: http://www.cnblogs.com/lijunamneg/archive/2013/02/05/2892616.html

Android--Startactivityforresult-------&&&----setresult

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.