Created
November 14, 2019 12:47
-
-
Save ntngel1/71c145d67ed1028f5fbbaf6306a53232 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T> Fragment.argument(name: String? = null): ReadOnlyProperty<Fragment, T> = | |
object : ReadOnlyProperty<Fragment, T> { | |
override operator fun getValue(thisRef: Fragment, property: KProperty<*>): T { | |
return thisRef.arguments?.get(name ?: property.name) as T | |
} | |
} | |
class FullscreenImagesDialogFragment : DialogFragment() { | |
private val something by argument<String>(SOMETHING_KEY) | |
// Либо можно просто: | |
private val something by argument<String>() | |
// Это эквивалентные записи так как SOMETHING_KEY = "something", а "something" - это идентификатор переменной. | |
// В случае, если идентификатор переменной совпадает с ключом в Bundle, то можно не передавать в argument<>() | |
// ключ по которому нужно вытащить поле | |
companion object { | |
private const val SOMETHING_KEY = "something" | |
fun newInstance(something: String) = FullscreenImagesDialogFragment().apply { | |
arguments = bundleOf(SOMETHING_KEY to something) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment