0

I have a ViewSwitcher inside a DialogFragment. The switcher holds 2 views, a "1. selection view" and a "2. selected view".

The initial view is the defaulted to "1. selection view". When fromType is non-zero from the observing event database, I want the Livedata observer to call dialogViewSwitcher.showNext(), but the showNext() function is not called on dialog's initialization.

DialogFragment, inside onCreateDialog:

viewModel.event.observe(this, Observer {
            it?.let {
                viewModel.setCurrentFromType(it.fromPlaceType)
            }
        })

viewModel.currentFromType.observe(this, Observer {
            if (it == 0) {
                viewModel.setCurrentFromStatus(false)
            } else {
                viewModel.setCurrentFromStatus(true)
            }
        })

viewModel.currentFromStatus.observe(this, Observer {
            binding.dialogViewSwitcher.showNext() 
        })

ViewModel:

private val _currentFromType = MutableLiveData(0)
    val currentFromType: LiveData<Int>
        get() = _currentFromType

    fun setCurrentFromType(fromType: Int) {
        _currentFromType.value = fromType
    }

private val _currentFromStatus = MutableLiveData(false)
    val currentFromStatus: LiveData<Boolean>
        get() = _currentFromStatus

    fun setCurrentFromStatus(status: Boolean) {
        _currentFromStatus.value = status
    }

Log shows the currentFromStatus observed the change (from false to true) when the dialog opens, but I'm thinking that because the dialogViewSwitcher hasn't initialized yet inside onCreatDialog when the change to currentFromStatus was observed, so showNext() did nothing. I also looked into data binding and didn't find a ViewSwitcher property in xml that shows the second view. What should I do to fix this behaviour?

PhantomCosmos
  • 119
  • 1
  • 7

1 Answers1

0

Didn't find a good way to programmatically showNext() at onCreate. Instead I deleted the viewswitcher and use databinding on the visibility of each of the 2 child views, and this way works well.

PhantomCosmos
  • 119
  • 1
  • 7