首页 > 解决方案 > Android navigation component- With Login screens

问题描述

When dealing with login screens, I am trying to work out the better approach - either execute navigation "action" to go to login fragment on first use (and hide back button to actual app), or start a new login activity (with its own nav graph). For the first approach (just using navigation components), I do not know the way to remove the back button without a hack "hide". I tried using navoptions, setpopupto etc., but it does not work. Code below:

val navOptions = NavOptions.Builder()
                .setPopUpTo(R.id.home_fragment, true)
                .build()

host?.navController?.navigate(R.id.action_global_signUpFragment_dest, null, navOptions)

Two questions then: 1) How to properly handle login transition with just navigation component? 2) Is starting a new login activity, with separate nav graph, a better idea?

标签: android-architecture-navigation

解决方案


我认为第一种方法更好。要在 signUpFragment 内隐藏工具栏上的“后退”按钮,您可以使用 AppBarConfiguration,并自定义哪些目的地被视为顶级目的地。例如:

val appBarConfiguration = AppBarConfiguration.Builder(setOf(R.id.home_fragment, R.id.signUpFragment_dest)).build()
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration)

这样 home_fragment 和 signUpFragment_dest 将被视为顶级目的地,并且工具栏上没有后退按钮。


推荐阅读