I have a fragment that starts a DialogFragment with a DatePickerDialog. I want to pass the date back to the host fragment. I saw someone use setTargetFragment but that is deprecated and on the docs for that function it said to use setFragmentResultListener, but the FragmentResultListener.onFragmentResult function seems to not get called.
What am I doing wrong and what is good convention?
Host Fragment:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
childFragmentManager.setFragmentResultListener(
REQUEST_DATE,
viewLifecycleOwner,
this
)
}
override fun onFragmentResult(requestKey: String, result: Bundle) {
Log.d(TAG, "Key: $requestKey, Bundle: $result")
}
Dialog:
val requestKey = requireArguments().getString(
ARG_REQUEST_KEY,
""
)
val bundle = Bundle().apply {
putSerializable(requestKey, resultDate)
}
requireParentFragment().setFragmentResult(
requestKey,
bundle
)
Also, I am passing the host fragment's childFragmentManager when calling the DialogFragment.show function.