How Get Result From Onactivityresult In Fragment?
Solution 1:
In Activity class:
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode,resultCode,data);
}
In Fragment :
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data){}
Option 1 :
If you're calling startActivityForResult()
from the fragment then you should call startActivityForResult()
not getActivity().startActivityForResult()
, as it will result in fragment onActivityResult()
.
If you're not sure where you're calling on startActivityForResult()
and how you will be calling methods.
Option 2:
Since Activity gets the result of onActivityResult()
, you will need to override the activity's onActivityResult()
and call super.onActivityResult()
to propagate to the respective fragment for unhandled results codes or for all.
If above 2 options do not work, then refer option 3 as it will definitely work.
Option 3 :
Explicit call from fragment to onActivityResult function as follows
In Parent Activity class, override the onActivityResult()
method and even override the same in Fragment Class and call as the following code.
In Activity:
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
Fragmentfragment= getSupportFragmentManager().findFragmentById("yourFragment");
fragment.onActivityResult(requestCode, resultCode, data);
}
In Fragment:
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
//in fragment class callback
}
Solution 2:
Replace
getActivity().startActivityForResult(intent, CAMERA_DATA);
with
startActivityForResult(intent, CAMERA_DATA);
Solution 3:
onActivityResult should be implemented in Activity, this class is your Fragment. Apply onActivityResult inside FragmentActivity.
Post a Comment for "How Get Result From Onactivityresult In Fragment?"