Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: 'onCreateView' always returns non-null type

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.

like image 269
Arsen Tatraev Avatar asked Nov 30 '20 14:11

Arsen Tatraev


1 Answers

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 Activitys lifecycle. but in that case dev won't be overriding onCreateView at all

like image 192
snachmsm Avatar answered Nov 05 '22 19:11

snachmsm