androidfragmentbottom-sheetdialogfragment

Get value from Bottom Sheet Dialog Fragment


I'm starting bottomSheetDialogFragment from a fragment A. I want to select the date from that bottomSheetDialogFragment then set it in the fragment A.

The select date is already done, I just want to get it in the fragment A to set it in some fields.

How can I get the value? Any suggestions how to do it?


Solution

  • Create an interface class like this

    public interface CustomInterface {
    
        public void callbackMethod(String date);
    }
    

    Implement this interface in your Activity or Fragment. and make an object of this Interface.

    private CustomInterface callback;
    

    Initialize it in onCreate or onCreateView

    callback=this;
    

    Now pass this callback in your BottomSheetDialogFragment constructor when you call it.

    yourBottomSheetObject = new YourBottomSheet(callback);
    yourBottomSheetObject.show(getSupportFragmentManager()," string");
    

    Now in your BottomSheetFragment's constructor

    private CustomInterface callback;

    public SelectStartTimeSheet(CustomInterface callback){
    
    this.callback=callback;
    
    }
    

    And at last use this callback object to set your date

    callback.callbackMethod("your date");
    

    and yout will recieve this date in your Fragment or Your Activity in callbackMethod function.