0

I have read the solutions here but in my case, I would like to get the navHostFragment and then navigate to the next destination in the action. I want to move the onClick method to the ViewController.

SOME AVAILABLE CODE:


class SimpleViewModel : ViewModel() {
    //        private set // This is to prevent external modification of the variable.
    private val _flowStepNumberArg =  MutableLiveData(1)
    val flowStepNumberArg: LiveData<Int> = _flowStepNumberArg

    fun onFragOneButtonPressed() {
        _flowStepNumberArg.value = 2
    }

    fun onClickFriend() {
        Log.d("BindingAdapters", "BUTTON CLICKED")
    }
}

class HomeFragment : Fragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

      view.findViewById<Button>(R.id.navigate_action_button)?.setOnClickListener {
            val flowStepNumberArg = 1
            val action = HomeFragmentDirections.nextAction(flowStepNumberArg)
            findNavController().navigate(action)
        }
}

//in fragment_layout.xml::
    <data>
        <variable
            name="viewmodel"
            type="com.example.android.codelabs.navigation.data.SimpleViewModel" />
    </data>
    <Button
            android:id="@+id/next_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/next_step_button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text"
            android:onClick="@{() -> viewmodel.onClickFriend2()}"
            />
nyxee
  • 2,773
  • 26
  • 22

0 Answers0