Navigating navigation

Android Developers · 38K views · 2020 · 14:20

Open on YouTube

Software architecture & designSoftware (other)talkdated

Why Stack Overflow recommends it

Stack Overflow users link it as the official guidance on handling login and conditional navigation with the Navigation Component, including why login should not be the start destination and how to return a result to the previous destination.

Recommended by 3 people across 5 different months, 2020 to 2022.

What it is

A 14-minute Android Developers talk from the 11 Weeks of Android series in which Ian Lake covers the Navigation Architecture Component. It looks at improvements in Navigation 2.2 and 2.3: deep linking, returning results between destinations, testing support, and dynamic feature module integration.

What people said

Recommends ianhanniballake · 2020-08 · Stack Overflow · on “Nav component deeplink do not do conditional navigation”

@uberchilly - even if you use a separate activity, you absolutely must check whether you are logged in at every destination as per the documentation and the Navigating Navigation video [this video] .

Recommends ianhanniballake · 2020-08 · Stack Overflow · on “When I add the same fragment to second navigation graph, don´t recognize the Action Class”

You haven't really explained why you have two graphs in the first place. Also note that you should be handling login as per the conditional navigation doc [LINK] and specifically **not** as your start destination of your graph as per the Navigating Navigation talk [this video] .

Recommends ianhanniballake · 2020-10 · Stack Overflow · on “Fragment automatically jumps to startdestination fragment of navigation graph when app opens after opening app through a Firebase dynamic link”

"I have 2 navigation graphs. One for the login process and one for the main app." - this is not how you do login with the Navigation Component as per the documentation [LINK] and the Navigating Navigation video [this video] specifically because of how deep linking works.

Recommends ianhanniballake · 2020-10 · Stack Overflow · on “Navigation Components Child NavHost Fragments' view not shown in Parent NavHost Fragment View”

This isn't how you are supposed to do login with the Navigation Component as per the documentation [LINK] and the Navigating Navigation video [this video] .

Recommends Valeriy Katkov · 2020-11 · Stack Overflow · on “Navigation Architecture Component - Login screen”

Here's an official solution suggested by Ian Lake in Navigating navigation video [this video] at Jul 23, 2020 on Android Developers [LINK] YouTube channel. The solution is based on navigation 2.3 release [LINK] which introduced an ability to return a result [LINK] to the previous destination. In our case the login fragment returns LOGIN_SUCCESSFUL state to the previous destination, it might be the profile fragment or any other fragment which requires login. class LoginFragment : Fragment(R.layout.login) { ... override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val navController = findNavController() val savedStateHandle = navController.previousBackStackEntry?.savedStateHandle ?:

Recommends Roshan · 2022-02 · Stack Overflow · on “Android login/logout flow with the new SplashScreen API”

Homefragment should be the startDestination. Conditionally navigate to loginfragment and pop back to Homefragment after authentication. Refer to the following video by Ian Lake. [this video]

Recommends ianhanniballake · 2022-04 · Stack Overflow · on “Intercept a DeepLink with navigation component”

The Principles of Navigation [LINK] very specifically state that you should never be using login as your start destination and links to the guide on how you should be doing login [LINK] , which specifically handles these cases correctly as explained in the Navigation Navigation talk [this video] . Is there a reason you aren't following those docs?

Cites ianhanniballake · 2021-03 · Stack Overflow · on “Android Navigation: removing an activity from the backStack”

Each individual NavController is totally independent from one another. While an <activity> destination allows you to use navigate() to go to an entirely separate activity (which may or may not use Navigation itself), Navigation itself will never finish() an activity as part of a navigate() call so you'd need to do that yourself. Using multiple activities with different navigation graphs is not the recommended way to handle authentication in Navigation as per the the Navigating Navigation talk [this video] and this approach fails in many ways (such as deep linking and invalidation after process death/recreation) that are correctly handled by the guide for handling login [LINK] . When using one Nav

Cites Francesc · 2021-07 · Stack Overflow · on “how do I send my date from datepickerfragment to another fragment”

If you use the navigation component from the jetpack components you can use the getPreviousBackStackEntry() to populate, from your picker dialog, the launcher fragment's bundle with the result of the picker, then when you return to your launcher fragment you read the result from the current backstack entry using getCurrentBackStackEntry . You can reference this video for examples [this video] If you do not use the navigation component, then you could use a shared viewmodel scoped to the Activity. You would get a reference to this shared viewmodel in both of your fragments. Then the date picker would populate some field in your shared viewmodel and the launcher fragment would read that field when

Critical ianhanniballake · 2021-04 · Stack Overflow · on “Navigation component implicit deep link opens the screen with an empty backstack”

There are numerous problems [this video] with gatekeeping your deep links with an authentication screen. Is there a reason you aren't following the guide for login [LINK] ? Deep links automatically work correctly when you follow that guide.