arg

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

Lazily retrieve an argument passed to this fragment.

Throws an exception if the fragment's arguments do not contain a compatible value for the given bundleKey. Use argOrNull for a "safe" alternative if the bundleKey may not be present.

Return

a Lazy from arguments, stored with this bundleKey

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 ArgSampleFragment @FragmentInject constructor() : Fragment() {

  val nameArg: String by arg("name")

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

See also

for the non-throwing version

for all supported types and how they're treated

Parameters

A

the expected return type, which may be nullable only if a null value is being explicitly set in the arguments bundle. See bundleOf for all supported types and how they're treated.

bundleKey

key used to pass this argument

Throws

if this fragment's arguments is null

if either:

  • the argumentsbundle exists, but does not contain an entry for bundleKey

  • an entry for bundleKey exists, but its value is not of type A.

Sources

Link copied to clipboard