Android -- startActivityForResult and setResult

Source: Internet
Author: User

Android -- startActivityForResult and setResult
The difference between startActivityForResult and startActivity is that startActivity () Only jumps to the target page. If you want to jump back to the current page, you must use startActivity () again (). StartActivityForResult () can be used to complete this task at a time. When the program executes this code, if the T1Activity jumps to the next Text2Activity, And the Text2Activity calls the finish () method, the program automatically jumps back to T1Activity and calls the onActivityResult () method in the previous T1Activity. Instead of startActivity (intent), startActivityForResult (intent, 0) Intent intent = new Intent (); intent. setClass (. this, B. class); Bundle bundle = new Bundle (); String str1 = "aaaaaa"; bundle. putString ("str1", str1); intent. putExtras (bundle); startActivityForResult (intent, 0); // startActivityForResult is used for redirect. Here 0 is a basis and other values can be written, the onActivityResult method must be rewritten to be greater than or equal to 0 to receive data returned by B. Protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (resultCode) {// resultCode indicates the callback flag. In B, RESULT_ OK case RESULT_ OK is returned: bundle B = data. getExtras (); // data is the Intent String str = B. getString ("str1"); // str is the return value break; default: break;}. The setResult method is used to return data in B, and the finish method must be called later. SetResult (RESULT_ OK, intent); // intent is the intent with Bundle sent by A. Of course, you can also define A new Bundlefinish (); // you must call finish () here () method setResult () If setResult is set in the Activity in which startActivityForResult is obtained, the result will not be immediately returned to the Activity of the parent, only the current Activity is finished, the result will be sent to the onActivityResult of the parent for processing! 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 (). fi NishActivity (mToken, resultCode, resultData) {mFinished = true ;}} catch (RemoteException e) {// Empty }} else {mParent. finishFromChild (this) ;}} this Code shows that the result returned by the activity is completed, that is, the setResult () method must be called before finish. If setResult () is called in the following method, it may not return success: onPause (), onStop (), onDestroy (), because the call to these methods is not necessarily prior to the completion, of course, the call to setResult in onCreate () must be performed by pressing the BACK key before the completion to exit from an Activity, android automatically calls the finish () method of the Activity and sets resultCode to RESULT_CANCELED, so that no data is returned. the solution is to capture the BACK-based event in the Activity. After the event is captured, setResult is obtained first, and then finish is called by yourself ...... Directly swallowed the BACK event @ Override public void onBackPressed () {Log. I (TAG, "onBackPressed"); setResult (Const. LIVE_ OK); super. onBackPressed ();

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.