It is safe to assume that activity
either is context
not zero in any of the fragment life cycle methods (between onAttach
and onDetach)
). In this case
context!!
it seems better because if it is null, when it definitely should NOT be empty, something is terribly wrong and you should just crash.
In asynchronous callbacks, of course, check for null.
For me "!!" there will never be an option.
, , . , BaseFragment
:
val context: Context
@JvmName("getContext2")
get() = getContext()!!
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
context // Context is *never* null here.
}
fun someCallbackMethod() {
if (isAdded) {
context // Context is also not null here.
} else {
// But it is null here.
}
}
- , null-null assert.
, 27, . context
, , null .