不同片段上的onActivityResult

时间:2018-10-10 04:02:56

标签: java android

我在1个活动中有2个片段,即片段A和片段B。

从片段A移至带有按钮的片段B(添加片段),然后在片段B中使用startActivityOnResult()进行图像捕获/摄像头拍摄。

我可以在片段A中呼叫onActivityOnresult吗?

我想完成片段B,所以我得到了片段A上的imageUri

我已经在片段B中尝试了以下操作,但是没有用

getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();

你们能告诉我怎么做吗?

这是我的代码:

片段A:

 btnSelfieUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sessionHelper.setForceDontShowPinLockOnNextResume(true);
            if (shouldAskPermissions()) {

                askPermissions();
            }
            ((ApplicationActivity) mActivity).clearBackStack();
            ((ApplicationActivity) mActivity).clickTutorialSelfie();
            bSelfieUpload = true;

        }
    });
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {

        case TAKE_PICTURE_KTP:
            if (resultCode == Activity.RESULT_OK) {
                String path = data.getExtras().getString("uri");
                try {
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
                    fileKtp = PhotoUtils.convertToBase64(bitmap);
                    ivKtpDone.setVisibility(View.VISIBLE);
                    Glide.with(mActivity)
                            .load(path)
                            .asBitmap()
                            .skipMemoryCache(true)
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .error(getResources().getDrawable(R.drawable.default_photo))
                            .centerCrop()
                            .into(ivKtpDone);
                } catch (Exception e) {
                    Toast.makeText(mActivity, "Gagal memuat", Toast.LENGTH_SHORT).show();
                }
            }
            break;

        case TAKE_PICTURE_SELFIE:
            if (resultCode == Activity.RESULT_OK) {
                String path = data.getExtras().getString("uri");
                try {
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
                    fileSelfie = PhotoUtils.convertToBase64(bitmap);
                    ivSelfieDone.setVisibility(View.VISIBLE);
                    Glide.with(mActivity)
                            .load(path)
                            .asBitmap()
                            .skipMemoryCache(true)
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .error(getResources().getDrawable(R.drawable.default_photo))
                            .centerCrop()
                            .into(ivSelfieDone);
                } catch (Exception e) {
                    Toast.makeText(mActivity, "Gagal memuat", Toast.LENGTH_SHORT).show();
                }
            }
            break;
    }
}

片段b:

public void takePhoto() {
    sessionHelper.setForceDontShowPinLockOnNextResume(true);
    HCIDAppController.getInstance().setEnablePinLock(false);
    if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.N)) {
        Intent intent = new Intent(mActivity, HCIDCameraActivity.class);
        intent.putExtra("cameraId", TAKE_PICTURE);
       startActivityForResult(intent, 4);
    } else {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        photo = new File(Environment.getExternalStorageDirectory(), "/" + Math.random() + ".jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        startActivityForResult(intent, 4);
    }
    getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
}

1 个答案:

答案 0 :(得分:2)

您将在活动内部获得 onActivityResult()回调,因为两个片段都是同一活动的一部分。

一旦在 onActivityResult()内部被调用,请检查请求代码并弹出当前片段(片段B)。

因此,现在您的活动只有片段A,因此在FragmentA内创建另一个onActivityResult()方法并执行操作。

请参阅下面的链接以弹出片段。

How to close the current fragment by using Button like the back button?