• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

Is it possible to get a NavigationController and perform navigation with only a Context? Right now it appears no. That will require such a major restructuring of our app that I feel I must be missing something.

If the user is logged out we need to navigate to the LoginFragment. This logout check happens at several places that are not an Activity or Fragment. For example the Authenticator that goes in the OkHttpClient.

Currently this works fine, because you can access the ApplicationContext from anywhere, and that is all you need to create an Intent, startActivity with it, and add a new Activity on top the backStack.

    • The only workaround I can see is to convert all of the legacy code in the app to use Navigation Architecture Component, add it all to my nav graph, making one large graph. Then I'd only need one NavigationController for the entire app. I could make it visible everywhere by storing it in the Application, similar to how I'm currently storing the Context there. I really want to avoid changing all that legacy code though.

As per the Implementing Navigation documentation:

Note: The Navigation components are designed for apps that have one main activity with multiple Fragment destinations. The main activity hosts the navigation graph and is responsible for swapping destinations as needed. In an app with multiple activity destinations, each additional activity hosts its own navigation graph.

Therefore, no. There is no global NavController that exists for your entire app - each Activity would have its own NavController and associated navigation graph.

However, there's no requirement that you switch your entire app over to Navigation all at once. As per the Migrate to Navigation documentation, you could convert one Activity at a time, continuing to use a separate login activity for this particular case just as before.

This is helped by the built in support for <activity> destinations, which are destinations that when you navigate() to them, do a startActivity() call, rather than a FragmentTransaction. This would be useful if you have a 'Login' button in your UI that you'd want to tie to starting your login activity.

There are some very good reasons to move towards a Single Activity model though, as explained in the Single Activity: Why, When, and How talk, but the end of the talk does stress that:

If you find yourself contorting your own experience and it's not making sense to you, don't do it. If something is working, that's good. Keep it working. But if you're finding you're running into issues, you're having inconsistent behavior, or you want to do things like share ViewModels, maybe that's the time to think about moving towards a single Activity structure.

  • 1
Reply Report