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