I was under the impression that using LiveEvent, we would be able to use these events to trigger dialogs in a Fragment. E.g.
class MyFragment : Fragment(){
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val binding = // inflate view using Databinding
binding.viewModel = // some viewModel
viewModel.events().observe(viewLifecycleOwner, Observer { event ->
showDialog(event)
})
return binding.root
}
}
class MyViewModel : ViewModel(){
private val events = MutableLiveData<DialogEvent>()
fun events(): LiveData<DialogEvent> = events.toSingleEvent()
fun onClick() = events.postValue(DialogEvent)
}
object DialogEvent
So, whenever I click a button, a dialog is shown.
However, when I rotate the screen, the dialog is shown again.
This makes sense, because on orientation change, the observer is removed, added again, and will receive the latest value...