2

In Activity A, I want to open a dialog (CustomDialog). Inside CustomDialog, it has a button to open a camera. But the onActivityResult not getting called after I pick an image from gallery. No toast is getting displayed.

Activity A

private void openDialog() {
        CustomDialog alert = new CustomDialog();
        alert.showDialog(this);
    }

CustomDialog

public class CustomDialog extends Activity{

    Activity activity;
    ImageView imageView;

    public void showDialog(Activity activity) {
        this.activity = activity;
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.custom_dialog);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setCanceledOnTouchOutside(true);

        imageView = (ImageView) dialog.findViewById(R.id.logoApp);

        Button galleryBtn = (Button) dialog.findViewById(R.id.galleryBtn);

        galleryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                galleryIntent();
            }
        });
        dialog.show();
    }

    private void galleryIntent() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);//
        activity.startActivityForResult(Intent.createChooser(intent, "Select File"), 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(activity,"sdddddsss",Toast.LENGTH_LONG).show();
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                onSelectFromGalleryResult(data);
            }else{
              // ...
            }
        }
    }

    @SuppressWarnings("deprecation")
    private void onSelectFromGalleryResult(Intent data) {
        Bitmap bm=null;
        if (data != null) {
            try {
                bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        imageView.setImageBitmap(bm);
    }
}

I follow this http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample

Tony
  • 2,515
  • 14
  • 38
  • 71

3 Answers3

4

When you show dialog in Activity A, you set reference to Activity A as param: alert.showDialog(this); Then inside CustomDialog, you save this reference as activity variable:

public void showDialog(Activity activity) { this.activity = activity; ...}

This means, that this.activity is instance of Activity A. Later in your galleryIntent(), you start activity for result like this:

private void galleryIntent() { ... activity.startActivityForResult(Intent.createChooser(intent, "Select File"), 1); }

This means that onActivityResult will be called in Activity A, not your Custom dialog, because you've used activity variable.

You have 2 options for fix:

1) replace activity.startActivityForResult with CustomDialog.this.startActivityForResult

2) move your onActivityResult code from CustomDialog into Activity A

Kubik
  • 610
  • 7
  • 15
  • My project flow is like this: From firstActivity--> Activity A--> then click Activity A's button to open Custom Dialog. Next in the dialog select image ---> display in Activity A ---> back selected image to firstActivity. Everything worked fine – Tony Oct 27 '18 at 14:56
  • But after this, when I click firstActivity--> Activity A--> then back to firstActivity again, the image is gone. Why would this happened ? – Tony Oct 27 '18 at 14:57
  • It's hard to tell without seeing code of Activity A and firstActivity. How do you store image? Is it only in memory? Maybe your activity is destroyed/recreated when you go back? Or maybe you return empty bitmap during your second transition from Activity A back to firstActivity (when you didn't select anything via dialog)? – Kubik Oct 27 '18 at 15:04
  • My code is not much. Can I send my project to github for you to check, please? – Tony Oct 27 '18 at 15:05
  • Sure, send me link to your project. – Kubik Oct 27 '18 at 15:06
  • Yes, I have read your code and fixed the issue. Can I push my changes into your git repo? I have created account on gitlab, https://gitlab.com/kubik161 . If you set me WRITE access I can push the fix. – Kubik Oct 27 '18 at 15:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182642/discussion-between-tony-and-kubik). – Tony Oct 27 '18 at 16:00
1

You are calling: startActivityForResult on activity reference which is not the same as the CustomDialog in which you expect onActivityResult to be called. I dont remember ever calling startActivityForResult on instance of activity which is not foreground - I am not sure whether it will work. I suggest you to change: activity.startActivityForResult to startActivityForResult

btw. in your onActivityResult you have called super.onActivityResult(requestCode, resultCode, data); twice. This is not correct.

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

the reason onActivityResult() is not invoke because onActivityResult() is invoked when you start an activity for result i.e startActivityForResult(...) then when the started activity returns or is finished onActivityResult() is invoked

  • https://stackoverflow.com/a/31279458/6565937 please see link on way of writing CustomDialog and this link for proper use of activityForResult() https://stackoverflow.com/a/13433770/6565937 – Goodman Khumalo Oct 27 '18 at 12:41