I want to use DateDialogFragment and put the date I get there to another fragment
How do I put the set date after getting the calendar instance?
class DateDialogFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
private val calendar = Calendar.getInstance()
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
return DatePickerDialog(requireActivity(), this, year, month, dayOfMonth)
}
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, month)
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val selectedDate = calendar.time
val selectedDateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.JAPAN).format(calendar.time)
val bundle = Bundle()
bundle.putString("SELECTED_DATE", selectedDateFormat)
bundle.putParcelable("SELECTED_DATE_PARCELABLE", selectedDate)
setFragmentResult("REQUEST_KEY", bundle)
}
}
I found something called parcelable, but I don't know how to use it!
I tried to use the Long type to handle dates, but the database could not handle data comparison, so I tried to use SQLite's Parcelable.
CodePudding user response:
You can pass data between fragments as an argument: https://developer.android.com/guide/navigation/navigation-pass-data?authuser=1
Or another option: How to pass values between Fragments