Skip to content

Nullable Parameter in Fragment.arg(...) causes IllegalStateException #414

@oldwomanjosiah

Description

@oldwomanjosiah

When using nullable fragment args, the following works:

@ProvidesFragment(AppScope::class)
class SomeFragment @FragmentInject constructor() : Fragment() {

  val viewModel: SomeViewModel by tangleViewModel()

  @FragmentFactory
  interface Factory {
    fun create(
      @TangleParam(name = "NullableArg")
      nullable: SomeSerializable?
    ): SomeFragment
  }
}

class SomeViewModel @VMInject constructor(
  @TangleParam(name = "NullableArg")
  nullable: SomeSerializable?
) : ViewModel() {
  init {
    nullable?.let { Log.d("app", "$nullable") }
  }
}

but if you add

val nullable by arg<SomeSerializable?>("NullableArg")

you get a subclass type error (SomeSerializable? is not a subclass of Any)

and if you add

val nullable: SomeSerailizable? by arg("NullableArg")

you get no type errors, but at runtime, the app crashes with

java.lang.IllegalStateException: Bundle does not contain key: NullableArg

due to nullability being used as a sentinel for illegal construction in Fragment.arg

This can be worked around by having a non-nullable data class to wrap the optional parameter, but this feels inelegant when you only need one nullable arg. Is it possible to relax the constraints so that they match those of the ViewModel?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions