how to implement Conditional Navigation in navigation architecture components

后端 未结 2 2057
无人及你
无人及你 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:24

    This is how I deal with conditional navigation :

    1. Set HomeFragment as the start destination
    2. Create a global action for LoginFragment

      
      
    3. Perform conditional navigation inside onViewCreated :

      // HomeFragment.kt
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
      
          if(!appAuth.isAuthenticated()) {
              view.findNavController().navigate(R.id.action_global_loginFragment)
          }
      }
      

提交回复
热议问题