2

I am using Navigation Component in my app, and I'm using it to handle my deep links. It's working great, but as it stands, it really only works as intended if my user is logged in. That's because I have the deep link declared in a module that assumes the user is authenticated. Authentication is done in another module.

Here is a rough diagram of my navigation graph for my single Activity app.

NavHostFragment (app module, MyActivity)
|
|--- MyLoginFragment (login module, login_navigation.xml)
|         |
|         `---...
|
|--- MyHomeFragment (home module, home_navigation.xml)
|         |
|         `--- MyFinalFragment (my-app://deep-link/uri)
.

I have a deep link set up in home_navigation.xml inside the HomeFragment <fragment> element.

I need to intercept this deep link, possibly in the Activity, and route them back to the login module when the user is not logged in.

Is there a Navigation Component idiom for this? Or am I forced to check the Intent data in onCreate of the MyHomeFragment/MyActivity and manually navigate?

Tyler
  • 19,113
  • 19
  • 94
  • 151
  • 1
    The [Principles of Navigation](https://developer.android.com/guide/navigation/navigation-principles#fixed_start_destination) 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](https://developer.android.com/guide/navigation/navigation-conditional#login), which specifically handles these cases correctly as explained in the [Navigation Navigation talk](https://youtu.be/09qjn706ITA?t=273). Is there a reason you aren't following those docs? – ianhanniballake Apr 19 '22 at 23:18
  • @ianhanniballake, thanks for the direction. The app is behind a paywall, so the first screen they see is login. We started with Home as the entry point but we noticed a lot of UI glitch when having to push the Login fragment immediately. I'm now revisiting our approach to have Home the main entry point. – Tyler Apr 21 '22 at 04:55
  • If you are are using any modern version of Fragment (i.e., Fragment 1.3 or higher), there's no glitch in redirecting to another fragment in `onViewCreated()`. Home should definitely be the start destination of your graph. – ianhanniballake Apr 21 '22 at 05:10

0 Answers0