1

I have a 2 fragments in 1 activity, namely, fragment A and fragment B.

From fragment A, I move to fragment B with button (add fragment) and in fragment B I use startActivityOnResult() to intent image capture / camera.

Can I call onActivityOnresult in fragment A?

I want to finish the fragment B, so I get the imageUri on fragment A.

I have tried the following in fragment B, but not working

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

Can you guys tell me how to do it properly?

Here is my code:

Fragment 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 :

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();
}
Henry Woody
  • 14,024
  • 7
  • 39
  • 56
Borom1r
  • 155
  • 1
  • 7
  • 18

1 Answers1

2

You will get onActivityResult() callback inside your activity as both fragments are part of the same activity.

Once you get called inside onActivityResult() check the request code and pop the current fragment(Fragment B).

So now you have only fragment A on your activity, so create another method of onActivityResult() inside your FragmentA and perform your operation.

Refer below link for pop up the fragment.

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

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147