简体   繁体   中英

Why I'm not receiving any results from onActivityResult

I'm starting DialogFragment from AddFragment by calling showPickImageDialog() method on button press. And in AlertDialogFragment onActivityResult I'm sending results back to AddFragment but AddFragment is not receiving any results. Any ideas why?

AddFragment:

    public class AddFragment extends Fragment {

    private static final int REQUEST_IMAGE = 3;
    private static final String DIALOG_IMAGE = "image";
    private static final String TAG = "TAG";

    void showPickImageDialog() {
        FragmentManager fm = getActivity()
                .getSupportFragmentManager();
        AlertDialogFragment pdialog = new AlertDialogFragment();
        pdialog.setTargetFragment(this, REQUEST_IMAGE);
        pdialog.show(fm, DIALOG_IMAGE);
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


            if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){

                Log.e(TAG,"back in AddFragment");

            }
        }
}

In AlertDialogFragment Im calling sendResult method from onActivityResult method. SendResult method is called I checked it with Logs

AlertDialogFragment:

public class AlertDialogFragment extends DialogFragment {

private final static String TAG = "Cload";
public static final String EXTRA_PHOTOFILENAME =
        "com.example.tadas.dreamcload1.image";
private static final int REQUEST_IMAGE = 3;
private String filename;
private Uri uri;


private void sendResult(int resultCode) {
    if(getTargetFragment() == null){return;}

    Intent i = new Intent();
    i.putExtra(EXTRA_PHOTOFILENAME,filename);
   getTargetFragment()
           .onActivityResult(getTargetRequestCode(), resultCode, i);

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    //LayoutInflater inflater = getLayoutInflater();
   // View dialogLaout = inflater.inflate(R.layout.custom_dialog_pick_image,null);
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.custom_dialog_pick_image, null);
    final AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setView(view)
            .create();
    ImageButton gallerybtn = (ImageButton)view.findViewById(R.id.gallery);
    ImageButton camerabtn = (ImageButton)view.findViewById(R.id.camera);

    gallerybtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e(TAG,"gallery btn pressed");
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  startActivityForResult(intent, REQUEST_IMAGE);
        }
    });
    camerabtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


    return dialog;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){

    uri = data.getData();

        final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
        Cursor c = getActivity().getContentResolver()
                .query(uri,filePathColumn,null,null,null);

        if (c.moveToFirst()){
            int ids = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            uri = Uri.parse(c.getString(ids));
            filename = uri.toString();
            c.close();
        }
        sendResult(Activity.RESULT_OK);
        dismiss();
    }

}

You can override your onActivityResult method in your Activity and call your Fragment's onActivityResult manually like below:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_container);
    fragment.onActivityResult(requestCode, resultCode, data);
}

You can define this part in your Activity's onActivityResult method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK){

    uri = data.getData();

        final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
        Cursor c = getActivity().getContentResolver()
                .query(uri,filePathColumn,null,null,null);

        if (c.moveToFirst()){
            int ids = c.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            uri = Uri.parse(c.getString(ids));
            filename = uri.toString();
            c.close();
        }
        // You do not need this any more
        //sendResult(Activity.RESULT_OK);
        //dismiss();
        // Just find your fragment and call it's onActivityResult
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your_container);
        fragment.onActivityResult(requestCode, resultCode, data);
    }

}

I think this change'll help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM