onActivityResult in dialog fragment

我只是一个虾纸丫 提交于 2019-12-10 16:38:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!