Android performs a simple workaround onactivityresult not be called in fragment

Source: Internet
Author: User

In Android development, we often use fragmentactivity to nest multiple fragment, but in the development process it is found that using Onactivityresult callback method in nested fragment is not executed.

There are a lot of solutions on the Internet, but they are more troublesome, so today we recommend a super-simple usage,

The Onactivityresult method is rewritten in both fragment and fragmentactivity, and the request code or the result code of the two are guaranteed to be identical. The code is as follows:

In the Fragmentactivity

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Switch(resultcode) { Case1://The result code should be consistent with the fragment//What I'm using here is to get the data based on the result code, and then add the following line of code, which//He doesn't have to do anything.               Super. Onactivityresult (Requestcode,resultcode,data);  Break;  Case2:                Super. Onactivityresult (Requestcode,resultcode,data);  Break;    ......        } }

In the Fragment

  @Override  public  void  onactivityresult (int  Requestcode, int   ResultCode, Intent data) { switch   (ResultCode) { case  1://  The result code is consistent with the fragmentactivity  //  here            Take the data you need  break  ;  case  2:  break  ; }    }

In summary to solve the problem of using onactivityresult not to be called in fragment, only need to do in fragment and fragmentactivity simultaneously rewrite Onactivityresult method, And make sure that the request code or result code used is the same.

is not very simple, have the need of friends can refer to.

Android performs a simple workaround onactivityresult not be called in fragment

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.