问题
I'm taking photo from dialog fragment. And also I need something like startActivityForResult(takePictureIntent, actionCode);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
getActivity();
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
setPic();
}
break;
case ACTION_TAKE_PHOTO_B: {
getActivity();
if (resultCode == Activity.RESULT_OK) {
handleBigCameraPhoto();
}
break;
}
}
}
Bu this method is not being called. Are there any method like this which can be used in dialog fragment??
回答1:
try like this :
to start activity from fragment :
getActivity().startActivityForResult(intent, code);
to get result back in fragment :
in your parent activity (fragment call activity) :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
fragmentObject
.onActivityResult(requestCode, resultCode, data);
}
回答2:
All you need to do is implement the hosting activity's onActivityResult
and call its base class implementation like below...
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//this makes sure the child fragments receive this event
super.onActivityResult(requestCode, resultCode, data);
...
}
You don't really need a reference to the fragment(s). Calling the activity's base class implementation will ensure all fragment's receive the call to onActivityResult
来源:https://stackoverflow.com/questions/22557965/onactivityresult-in-dialog-fragment