0

I have created a bottom fragment and now i need some data from bottom fragment in the main activity when a button is clicked in bottom fragment. I saw few answers by creating interface but not very clear with the concept.

Code:

Main activity using this function to call fragment:

public void showSchedule() {


    BottomSheetFrag bottomSheetFragment = new BottomSheetFrag();


    Bundle bundle = new Bundle();
    bundle.putString("UserId", Parlour_BeauticianID);

    bottomSheetFragment.setArguments(bundle);
    bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());


}

Fragment:

public class BottomSheetFrag extends BottomSheetDialogFragment {
public static BottomSheetFrag newInstance() {
    return new BottomSheetFrag();
}

public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.activity_appointment, container, false);

    Bundle bundle = getArguments();
    userID = bundle.getString("UserId");


    ImageView imageViewClose = (ImageView) view.findViewById(R.id.imageClose);
    final Fragment f = getActivity().getFragmentManager().findFragmentById(R.id.Linear_layout);


    imageViewClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dismiss();

        }
    });

button9AM1 = (Button) view.findViewById(R.id.button9AM1);

 /* when i click the button9AM1 i want to send the data as as 9AM to main activity */

 }} 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ash
  • 17
  • 4
  • I'm not sure but check this post to see if this is what you need [Passing data between a fragment and its container activity](https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – TRose Feb 27 '20 at 07:14

1 Answers1

0
interface OnclickDataLisnter{
   void onClick(String data);
}

and

 public void showSchedule() {


BottomSheetFrag bottomSheetFragment = new BottomSheetFrag();

bottomSheetFragment.setClickListner(new OnclickDataLisnter(){
                                       @Overrid
                                        public void onClick(String data){
                                         //You listner here your data
                                         }
                                    }
Bundle bundle = new Bundle();
bundle.putString("UserId", Parlour_BeauticianID);

bottomSheetFragment.setArguments(bundle);
bottomSheetFragment.show(getSupportFragmentManager(), 
bottomSheetFragment.getTag());

}

and

     public class BottomSheetFrag extends 
          BottomSheetDialogFragment {
          public OnclickDataLisnter clickListner;
          public static BottomSheetFrag newInstance() {
            return new BottomSheetFrag();
          }
          public void setClickListner(OnclickDataLisnter clickListner){//add this
            this.clickListner = clickListner; // add this

            }
         public View onCreateView(LayoutInflater inflater,
                     @Nullable ViewGroup container,
                     @Nullable Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.activity_appointment, container, false);

      Bundle bundle = getArguments();
    userID = bundle.getString("UserId");


      ImageView imageViewClose = (ImageView) view.findViewById(R.id.imageClose);
       final Fragment f = getActivity().getFragmentManager().findFragmentById(R.id.Linear_layout);


   imageViewClose.setOnClickListener(new View.OnClickListener() 
   {
    @Override
    public void onClick(View v) {

        dismiss();

        }
   });

    button9AM1 = (Button) view.findViewById(R.id.button9AM1);

        /* when i click the button9AM1 i want to send the data as as 9AM to main activity */
        clickListner.onClick(data); //add this

  }} 
ashok
  • 431
  • 5
  • 8
  • Thank you so much. I saw many posts with interface logic but i was missing how to implement it when i click on a button. This was very clean and solved my problem. – ash Feb 27 '20 at 08:23