1

I am trying to pass data from dialog to bottom sheet fragment. Here is how my bottom sheet fragment opens my dialog fragment

     editAddDate.setOnClickListener{
             val timeDatePickerDialog = TimeDatePickerDialog(requireContext())
             timeDatePickerDialog.setOnDismissListener(this)
             timeDatePickerDialog.show()



         }

in my dialog fragment I implemented Dialog(context),TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener as the following :

class TimeDatePickerDialog(context: Context) : Dialog(context),TimePickerDialog.OnTimeSetListener,
    DatePickerDialog.OnDateSetListener {

    private lateinit var pickDate: TextView
    private lateinit var pickTime: TextView
    private lateinit var repeat: TextView
    private lateinit var done: TextView
    private lateinit var cancel: TextView
    private lateinit var selectedDate: String
    private lateinit var selectedTime: String
    private lateinit var binding: TimeDatePickerDialogBinding


    var day=0
    var month=0
    var year=0
    var time=0
    var minutes=0

    var savedDay=""
    var savedMonth=""
    var savedYear=""
    var savedTime=0
    var savedMinutes=0



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.time_date_picker_dialog,
            null,
            false
        )

        setContentView(binding.getRoot())



        pickDate=binding.setDate
        pickTime=binding.setTime
        repeat=binding.repeat
        cancel=binding.cancel
        done=binding.done

        initView()


    }


    override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
        savedTime=hourOfDay
        savedMinutes=minute
        var time="$savedTime:$savedMinutes"
        selectedTime=time
        pickTime.text=selectedTime
    }

    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        savedDay=dayOfMonth.toString()
        var currentMonth=month+1
        savedMonth=currentMonth.toString()
        savedYear=year.toString()
        var date="$savedDay,$savedMonth $savedYear"
        selectedDate=date

        pickDate.text = selectedDate

    }

    private fun getDateTimeCalendar(){
        val c=Calendar.getInstance()
        day=c.get(Calendar.DAY_OF_MONTH)
        month=c.get(Calendar.MONTH)
        year=c.get(Calendar.YEAR)
        time=c.get(Calendar.HOUR)
        minutes=c.get(Calendar.MINUTE)

    }

    private fun initView() {

        pickDate.setOnClickListener {
            getDateTimeCalendar()
            DatePickerDialog(context, R.style.PickerDialogTheme, this, year, month, day).show()

        }

        pickTime.setOnClickListener {
            getDateTimeCalendar()
            TimePickerDialog(context, R.style.PickerDialogTheme, this, time, minutes, true).show()
        }

        cancel.setOnClickListener {
            dismiss()
        }


       done.setOnClickListener {

            dismiss()}


    }


}



I want to send my date and time when done is clicked from this dialog to the bottom sheet dialog I cant find a way to do this I tried shared viewModels but I run into more issues than I expected can I do this in a simple way

Ryan M
  • 18,333
  • 31
  • 67
  • 74
goodee
  • 33
  • 7
  • Does this answer your question? [How to pass values between Fragments](https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) – Javad Dehban Jul 21 '20 at 10:53
  • is TimeDatePickerDialog your class? – Minki Jul 21 '20 at 10:55
  • @JavadDehban no because I am using host activity and multiple fragment I was able to pass data in the other fragments I used in my app by navigation component but I don't know why in dialog and bottom sheet I can't do that – goodee Jul 21 '20 at 10:59
  • @MingkangPan TimeDatePickerDialog is my class and it is declared as the following. class TimeDatePickerDialog(context: Context) : Dialog(context),TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener { – goodee Jul 21 '20 at 11:00
  • Why you don't send it as constructor parameter ? – Ali Khoshraftar Jul 21 '20 at 11:16
  • @AliKhoshraftar How? – goodee Jul 21 '20 at 11:18
  • Your TimeDatePickerDialog needs context as first parameter. you can create another constructor that get two parameters. one is context (to initialize your main constructor) and two is what ever you want and store it in a variable in TimeDatePickerDialog class and use it whenever you want. – Ali Khoshraftar Jul 21 '20 at 11:23
  • @AliKhoshraftar I got your point but how to pass this variable back to the parent fragment – goodee Jul 21 '20 at 11:27
  • @goodee if you need data back to parent fragment you have two choices : one is using broadcast and two is interface you pass as another parameter to TimeDatePickerDialog and implement it in parent fragment so you can always send data to parent fragment. – Ali Khoshraftar Jul 21 '20 at 11:33
  • So the thing that's probably confusing everyone trying to answer is that `TimeDatePickerDialog` is not actually a `DialogFragment`... I fixed your question to remove references claiming that it is. – Ryan M Jul 22 '20 at 04:38

2 Answers2

0

Simplest solution is just to define an interface:

class TimeDatePickerDialog(context: Context) : Dialog(context) {

    interface OnDateSelectedListener {
        fun onDateSelected(date: Date)
    }

    private lateinit var selectedDate : Date
    var onDateSelectedListener : OnDateSelectedListener? = null

    fun initView() {
        doneButton.setOnClickListener {
            dismiss()
            onDateSelectedListener?.onDateSelected(selectedDate)
        }
    }
}

Set the listener from your BottomSheetDialog after you instantiated the DatePicker Dialog

Minki
  • 934
  • 5
  • 14
0

Finally I found a way by using sharedViewModel but you have to extends DialogFragment() instead of Dialog(context) and the declare a variable to hold the sharedviewModel value that you created in my case : private val bottomSheetViewModel: BottomSheetViewModel by activityViewModels()

goodee
  • 33
  • 7