How do i get information from an activity that was called by a bottom sheet to the activity that called the bottom sheet

Viewed 41

Let me explain. I have an activity called Customer details which looks like this Customer Details

When you click the camera FAB that rests on the profile picture of the customer, a bottom sheet appears like this bottom sheet popup

I wrote the bottom sheet as an independent class so other activities that require media ( taking a photo etc.) could have access to it Here is the code segment that calls the bottom sheet

binding.customerDetailsDpFab.setOnClickListener(new 
View.OnClickListener() {
        @Override
        public void onClick(View view) {
            
bottomSheet.setLayout(R.layout.camera_bottom_sheet, 
newCustomer);
            
bottomSheet.show(getSupportFragmentManager(), 
"Customer details Bottom sheet");

        }
    });

}

and here is the bottom sheet Class

public class BottomSheet extends BottomSheetDialogFragment {
int layout;
boolean isNew;
MaterialTextView takePhoto, uploadPhoto, delete_cancelPhoto;
static final int PICK_IMAGE = 1;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(layout, container, false);
    takePhoto = view.findViewById(R.id.bottom_sheet_take_photo);
    uploadPhoto = view.findViewById(R.id.bottom_sheet_upload_photo);
    delete_cancelPhoto = view.findViewById(R.id.bottom_sheet_delete_photo);

    if(isNew) delete_cancelPhoto.setText(R.string.cancel);
    Log.d("text is ", delete_cancelPhoto.getText().toString());



    takePhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String text = "context is " + getContext().toString();
            Toast.makeText(getContext(),text, Toast.LENGTH_SHORT ).show();
            Intent i = new Intent(getContext(), CameraActivity.class);
            startActivityForResult(i, 100);
            getDialog().dismiss();

        }
    });

    uploadPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(getContext(), "Upload a photo", Toast.LENGTH_SHORT).show();
            getDialog().dismiss();
        }
    });

    delete_cancelPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String message;
            if(isNew) message = "Cancel";
            else message = "Delete photo";
            Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
            getDialog().dismiss();
        }
    });

    return view;
}

public void setLayout(int layout, boolean isNew) {
    this.layout = layout;
    this.isNew = isNew;
}

}

The bottom sheet inflates a layout passed to it and when you click "take photo", it starts the camera activity. This camera activity takes a photo, stores it, displays it on an image view, and asks the user if he is satisfied with the image taken. camera activity, on image captured

I have set a listener to the ok button so that it returns the Uri back to the calling activity like this

 case R.id.pic_ok:
            //send pic to activity
            Intent i = new Intent();
            i.setData(imageUri);
            setResult(RESULT_OK, i);
            finish();
            break;

And override onActivityResult in my CustomerDetails Activity like this

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 ){
        if (resultCode == RESULT_OK){
            Uri imageUri = data.getData();
            Toast.makeText(context, imageUri.toString(), Toast.LENGTH_SHORT).show();
            Glide.with(context)
                .load(imageUri)
                .into(binding.customerDetailsCustomerDp);
        }

    }

}

The problem is I can't seem to get the data (Uri of the saved image ) back to the customer detail activity. Please note the customerDetails Activity called the bottom sheet which in turn called the Camera activity using an intent. The bottom sheet is then dismissed when any of its options is clicked.

How do I get the Uri from the camera activity once it is finished and the user is satisfied with the image, back to customerDetails activity so i can set the Profile picture

0 Answers
Related