简体   繁体   中英

OnActivityResult not getting called

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

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

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.

之所以没有调用onActivityResult()的原因是因为当您为结果启动活动时即调用了onActivityResult(),即startActivityForResult(...),然后在启动的活动返回或结束时调用了onActivityResult()

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