argOrNull

inline fun <A> Fragment.argOrNull(bundleKey: String): Lazy<A?>

Lazily retrieve an argument passed to this fragment. Returns null if:

  • the fragment's arguments property is null

  • the fragment's arguments do not contain an entry for this bundleKey

  • the fragment's arguments contain an explicit null value for this key

  • the fragment's arguments contain a value for this key, but it cannot be assigned to type A

Return

a Lazy from arguments, stored with this bundleKey, if it exists. Otherwise null. Use arg if the argument is guaranteed to be present.

Samples

import androidx.fragment.app.Fragment
import tangle.fragment.ContributesFragment
import tangle.fragment.FragmentInject
import tangle.fragment.FragmentInjectFactory
import tangle.fragment.arg
import tangle.fragment.argOrNull
import tangle.inject.TangleParam
import tangle.inject.test.utils.AppScope
fun main() { 
   //sampleStart 
   @ContributesFragment(AppScope::class)
class ArgOrNullSampleFragment @FragmentInject constructor() : Fragment() {

  val nameArg: String? by argOrNull("name")

  @FragmentInjectFactory
  interface Factory {
    fun create(
      @TangleParam("name")
      name: String
    ): Fragment
  }
} 
   //sampleEnd
}

See also

for a non-nullable return when the argument is guaranteed

for all supported types and how they're treated

Parameters

A

the expected return type. See bundleOf for all supported types and how they're treated.

bundleKey

key used to pass this argument

Sources

Link copied to clipboard