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.
Related
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".
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.
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.
I have 3 fragments navigation each of them by 'Navigation.navigate' making network API to reload and viewpager to set again. I wanted to use the same fragment as it is, which was open already.
I am using the "Navigation Architecture Component", and I am using Navigation.navigate method
If you create your fragment each time a navigation happens you can instead use show() and hide() methods of the fragment, so whenever one of them is the visible show it and hide the other two and another way around. But if your problem is reloading data maybe you should consider other options. Like using ViewModel to store the data of the fragment. please provide more info and publish your code so we can help you better.
I have 2 Activities. One of it is displaying line chart, another is displaying pie chart. I would like to embed both in a single launched Activity. User is allowed to perform single click, to switch between line chart and pie chart.
I realize ActionBar's navigation tab, or drop-down navigation is the best candidate to help me achieve this purpose.
From Android API demo, and guideline found from http://developer.android.com/guide/topics/ui/actionbar.html, I realize all switched components are implemented as Fragment, not Activity
Does it mean that I need to port my previous 2 Activities into 2 Fragments, in order to embed them into ActionBar's tab navigation view/ drop-down navigation view?
Is there any other ways I can do without porting? But, is it advisable as I do not find an official example by using Activity.
In API demo, I realize most Fragment is implemented in the following pattern.
public class FragmentStack extends Activity {
...
public static class CountingFragment extends Fragment {
// CountingFragment never access any members in FragmentStack
}
}
Is there any reason to do so? Why don't they have CountingFragment is a separated file?
If you want to embed in a single activity, the preferred way is to use fragments. You could probably also use custom views, but Fragments have a well defined lifecycle and handle a lot thigs for you. So it might be a good idea to extract functionality into fragments, and have the activities only as shells, embedding the fragments (if you still need this with your new design). You can switch between fragments any way you like: with buttons, dropdowns or any other UI that fits your app; you don't necessarily have to use the action bar. As for 3. there is nothing wrong with defining fragments in separate files. Activity and fragment are in the same file in samples mostly to make it easier to follow the sample.