how to implement Conditional Navigation in navigation architecture components

后端 未结 2 2062
无人及你
无人及你 2021-02-08 14:17

In the new Navigation architecture component, how to implement conditional navigation?

Currently I have a single activity with LoginFragment and HomeFragment. Based on a

2条回答
  •  無奈伤痛
    2021-02-08 14:34

    You can add navigation listener inside your activity, and from there navigate to LoginFragment based on your conditions, like this:

    findNavController(nav_host_fragment).addOnNavigatedListener { controller, destination ->
            when(destination.id) {
                R.id.HomeFragment-> {
                 if(someCondition) {
                   //Navigate to LoginFragment
                 }
    
            }
        }
    

提交回复
热议问题