anyone know if theres a way to pass args into the ...
# android
v
anyone know if theres a way to pass args into the constructor of a fragment using the navigation component? I have a VehicleFragment that should take a vin. I know I can pass it in via a bundle but was wondering if constructor args are possible with the nav component
i
Arguments are for things that change from one instance to the next, so that is absolutely what you should be using for something like a vin - you might look at Safe Args for how to make that type safe with Navigation: https://developer.android.com/guide/navigation/navigation-pass-data#Safe-args
That being said, Navigation does support
FragmengFactory
, which would be how you could constructor inject in parameters from some outer scope (your activity, etc)
v
yes I know about safeArgs, I just wasnt sure if there was a way to pass in via the constructor
SafeArgs is somewhat messy because it generates all those Directions for every navigation action
i
I mean, yes, that's what you should be using to navigate all the time
If you aren't using that action, then delete the action
v
I disagree. I think avoiding code generation has a lot of benefits
i
I'm confused. In what cases would code be generated that you wouldn't actually be using?
v
Im not saying you wouldnt use the generated code, but the generated code could be avoided by using bundles and I think in general across all libraries there is benefit to avoiding code generation
its a tradeoff but the benefit in this particular case is not worth it imo but it might be worth it for others depending on their project and use case
i
Sure, that's why Safe Args is optional. You can certainly skip any sense of type safety and manually build and parse bundles and keep that code in sync without any help from the compiler.
I don't know why anyone would put themselves through that, but YMMV. Most of the code gets stripped away from R8 anyways
v
more concerned about build time. I would rather take the upfront time to write the bundle code (honestly not hard and what has been done for years prior to safeargs) than the build time cost on every single build
same reason im not a huge fan of dagger
the more code generation you can avoid the faster build and iteration time
i
It is fully incremental, only recompiling when your navigation graph file changes
So...maybe try it first and measure the impact
If you find a case where it isn't incremental at the navigation graph file level, please do file a bug: https://issuetracker.google.com/issues/new?component=409828&template=1093757