onActivityResult in dialog fragment
Asked Answered
I

2

6

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??

Innovation answered 21/3, 2014 at 11:50 Comment(1)
onActivityResult of fragment will not call directly, you have to call it from its parent activity.Sorcim
S
19

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);
}
Sorcim answered 21/3, 2014 at 11:59 Comment(2)
Hey man i tried your solution but it is showing the error that Fragment is not attached to Activity.Iridium
in your parent activity (fragment call activity) : how to call fragment call activity ?Revegetate
L
0

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

Libertinism answered 15/1, 2019 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.