I'm testing my fragments on espresso using something like this:
launchFragmentInContainer<ErrorManagerFragment>(themeResId = R.style.MaterialTheme)
However, my ErrorManagerFragment needs an argument and I can't figure out how to pass it to my fragment.
internal class ErrorManagerFragment : Fragment(R.layout.fragment_error_manager) {
private val navArgs: ErrorManagerFragmentArgs by navArgs()
private val attemptNumber by lazy { navArgs.StringNumAttemptError }
...
}
I tried a couple of solutions I found on the internet but they are not working as well.
Each NavArgs
class allows you to construct an instance directly from its values - i.e., you can use ErrorManagerFragmentArgs(15)
to construct a ErrorManagerFragmentArgs
instance. This can be transformed into a Bundle by using the toBundle()
method that every Args class has, allowing you to pass that to your launchFragmentInContainer
call:
val args = ErrorManagerFragmentArgs(15)
launchFragmentInContainer<ErrorManagerFragment>(
themeResId = R.style.MaterialTheme,
fragmentArgs = args.toBundle()
)