Today I found this warning in all my fragments:
Warning:(45, 12) 'onCreateView' always returns non-null type
onCreateView:
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
...
}
The question is, what has changed and why?
Android Studio version - 4.1.1.
this is Kotlin
warning, probably new lint check for helping you producing better quality code
your method returns declared View?
, but as we know in most cases this method shouldn't return null
View
, so this ?
(null safety) is redundant. just remove ?
, declare your method returns just View
(not null by default in Kotlin)
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View { // in here no "?"
...
}
btw. DOC is saying that this method may return null
and Fragment
behaves then just like some objects/data holder, without UI, just for proper retaining instance across Activity
s lifecycle. but in that case dev won't be overriding onCreateView
at all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With