I'm using the Android Jetpack Navigation component, and it seems like it's requiring that I pass the arguments in the wrong order.
I have two fragments (fragment1 and fragment2). Fragment 2 takes four arguments (bool, string, int, CustomParslableClass?). The IDE thinks that that is the correct order (and shows the red squiggles with a message "Type mismatch [...]" if I don't have them in that order). The app used to build fine with that order (a year ago when I last worked on it), but today I downloaded everything and had to update dependencies to build.
This is the normal line of code that the IDE thinks is right (used to work):
val direction = HomeViewPagerFragmentDirections.actionFragment1ToFragment2(false, "Favorites", 17, null)
However, when I build with that order, I get build errors for each argument:
e: filename.kt: (51, 106): The boolean literal does not conform to the expected type String
e: filename.kt: (51, 112): Type mismatch: inferred type is String but Int was expected
e: filename.kt: (51, 125): Type mismatch: inferred type is Int but PlaylistEntry? was expected
e: filename.kt: (51, 136): Null can not be a value of a non-null type Boolean
On the contrary, when I re-order the arguments to the function like so (arg2, arg3, arg4, arg1):
val direction = HomeViewPagerFragmentDirections.actionFragment1ToFragment2("Favorites", 17, null, false)
it builds fine, but the IDE shows red underlines under each argument, saying they're in the wrong order! (Type mismatch. Required: Boolean, Found: String
)
Maybe important:
- arg1 (bool) has a default value of false
- arg2 (string) has no default value
- arg3 (int) has no default value
- arg4 (CustomParslableClass?) has no default value (but is nullable)
When I change arg2 to have a default value, the IDE expects the same, but the order that works for build changes to (arg3, arg4, arg1, arg2)
.
A simple clean and rebuild didn't help. I also tried cleaning the project, shutting down Android Studio and restarting, then doing a build. I even tried deleting the action between the two fragments, and re-creating (building between times). All to no avail. Note that when I run the built app, everything works as it should Normally that would be fine, but I don't think the argument order is supposed to be jumping around like that!
CodePudding user response:
With multi args you should set like below
val action = BlankFragmentDirections.actionBlankFragmentToSecondFragment()
action.arg1 = 2
action.arg2 = 3
action.arg3 = "423"
findNavController().navigate(action)