简体   繁体   English

不同片段上的onActivityResult

[英]onActivityResult on different fragment

I have a 2 fragments in 1 activity, namely, fragment A and fragment B. 我在1个活动中有2个片段,即片段A和片段B。

From fragment A, I move to fragment B with button (add fragment) and in fragment BI use startActivityOnResult() to intent image capture / camera. 我从片段A移至带有按钮的片段B(添加片段),然后在片段BI中使用startActivityOnResult()进行图像捕获/摄像头操作。

Can I call onActivityOnresult in fragment A? 我可以在片段A中调用onActivityOnresult吗?

I want to finish the fragment B, so I get the imageUri on fragment A. 我想完成片段B,所以我在片段A上获得了imageUri

I have tried the following in fragment B, but not working 我已经在片段B中尝试了以下操作,但是没有用

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

Can you guys tell me how to do it properly? 你们能告诉我怎么做吗?

Here is my code: 这是我的代码:

Fragment A : 片段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;
    }
}

Fragment b : 片段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();
}

You will get onActivityResult() callback inside your activity as both fragments are part of the same activity. 您将在活动内部获得onActivityResult()回调,因为两个片段都是同一活动的一部分。

Once you get called inside onActivityResult() check the request code and pop the current fragment(Fragment B). onActivityResult()中被调用后,检查请求代码并弹出当前片段(片段B)。

So now you have only fragment A on your activity, so create another method of onActivityResult() inside your FragmentA and perform your operation. 因此,现在您的活动上只有片段A,因此在FragmentA内创建另一个onActivityResult()方法并执行操作。

Refer below link for pop up the fragment. 请参考下面的链接弹出片段。

How to close the current fragment by using Button like the back button? 如何使用后退按钮之类的按钮关闭当前片段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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