Navigation graph start destination issue - android

I am using Navigation Graph in an activity as it is the recommended approach to be used for fragments.
I only want to load fragment based on given condition otherwise do not load i.e., keep the startDestination as null.
Is it possible to do so? I don't want conditional start destination.

Related

popUpTo nested navigation graph ID not popping to startDestination

I'm trying to use popUpTo as part of configuring an action in androidx navigation to make sure the back button on the next screens returns to the specified fragment ID.
The problem is the fragment I want to navigate back to is defined as the startDestination of a nested graph. I would expect to be able to specify the ID of the nested graph in popUpTo, so that I don't have to link directly to a child fragment's ID, which I understand to be bad practice, if even not an error.
However, the only way to get the behavior I desire is by setting popUpTo to the ID defined as startDestination in the nested graph. Specifying the nested graph ID instead jumps over the fragments defined therein.
I could not find specific guidance on how to use popUpTo with nested graphs. Could anyone provide more details?
I would expect to be able to specify the ID of the nested graph in popUpTo, so that I don't have to link directly to a child fragment's ID, which I understand to be bad practice, if even not an error.
This should work correctly. If this does not work, it means something else is going wrong.
In my case, it turned out that elsewhere in the codebase the backQueue of NavController was manipulated manually (.clear()) was called.
After removing the offending code, I could update my popUpTo to refer to the nested graph ID, and the backstack got cleared as expected.

How to use navigation component with a parent fragment?

I have an app, which is a single-activity app. I use dagger2 for di, and an abstraction for navigation.
Let's see a navigation flow: auth - select-info (A, B) - menu. As you can see, there are three main destinations, and the second one contains two screens. I want to make "select-info" as a parent fragment for A and B, but the way navigation component works, I cannot implement that behavior.
I tried to google it, but something close to this issue is an approach to use fragmenManager to go to parentFragment and internally use navComponent, but this approach does not work in my case, because somehow I need to change a navGraph.
As an alternative I see using viewPager in "select-info".

How to reorganize messy android navigation graph?

So I use a single nav_graph.xml for the whole app. As the app grow it become messy jumble like shown below, and now it become so laggy to move around.
Is there any trick to untangle this thing without changing the code too much?
I also had this situation and it is normal , but you should refactor as it gets harder to make changes to that navigation graph or better understand your use cases and do it in the first place. Anyway the solution to that is you make a nested navigation graph for every feature of the app (if possible) , for example for profile option make a profile nested navigation graph now all fragment related to profile will go under that navigation graph and the navigation graph will become more manageable another advantage is that you can scope a viewmodel to the nested navigation graph, that means you can share data related to profile via that viewmodel.
As your navigation graph above how can you share data between those fragment , first you can use arguments but those are for limited data types , second you can have a viewmodel which is scoped to your single activity and and all your fragment share that data but that is also a really messy solution and huge impact on memory usage, since all memory used by activity viewmodel will remain consumed as long as the app is running.
So go will nested navigation graph, or if you want to have more activities you can also do that in that case other activities have their own navigation graphs.

How to use navigation architecture component to navigate dynamically

I have a use case and couldn't find an example of it online. So, my use case is I have a few fragments and the flow is decided by the server.Server returns a list of strings and based on the String, I have to navigate to a fragment. Eg:
Server returns ["detail_entry","accept_work","qr_code"] and depending on that I have to navigate using the navigation architecture component to DetailEntryFragment,AcceptWorkFragment or QRCodeScanningFragment. How to create Navigationaction directly in Kotlin code rather than use the navigation graph actions in xml.
I do not want to create an action between each and every fragment in xml and use it.I also want to mandate arguments passing. Is there any other way other of how to achieve this with the navigation architecture. I do not want to move to the old fragmentmanger and committing with them.

Circular Reference with Nested Nav Graphs

I have a complex navigation structure, I go from one nav graph then based on some logic, decide which nav graph to go to there. However both the nav graphs share 3 other nav graphs. See image below
Currently I get a circular reference error if I try to include each nav graph in the A & B. I have also tried creating a global action, which leads to illegal argument exception since it doesn't exist on the nav graph.
Please help!!!
I also don't know how to solve this. The Navigation Component should allow the reuse of modular graphs by passing some sort of ID from the initiating graph.
The only workaround I could think of was to create a copy of the graphs that have different origins.
For example, "Graph1" would now have two versions, "Graph1_from_GraphA" and "Graph1_from_GraphB". So "GraphA" and "GraphB" would reference them respectively.
If the graphs are not that far way, there is also the solution of conditional navigation, as described in the official documentation (https://developer.android.com/guide/navigation/navigation-conditional), in which we can pop the BackStack in the NavController with a result in the SavedStateHandle and then in the previous fragment we can redirect to the desired destination.

Categories

Resources