对话框片段中的onActivityResult

时间:2014-03-21 11:50:19

标签: android android-dialogfragment start-activity onactivityresult

我从对话框片段拍照。而且我需要像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;
            }
        }   
    }

不要调用此方法。有没有这样的方法可以在对话框片段中使用?

2 个答案:

答案 0 :(得分:17)

尝试这样:

从片段开始活动:

getActivity().startActivityForResult(intent, code);

将结果返回片段:

父活动中的

(片段调用活动):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    fragmentObject
            .onActivityResult(requestCode, resultCode, data);
}

答案 1 :(得分:0)

您需要做的就是实现托管活动的onActivityResult并按如下所示调用其基类实现...

@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);
    ...

}

您实际上并不需要引用这些片段。调用活动的基类实现将确保所有片段都收到对onActivityResult

的调用