December 2017
Intermediate to advanced
260 pages
7h 34m
English
If the following is the code in Kotlin for inflating layout for any fragment, do you see any possibility for improvement by applying any kind of extension function?
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater?.inflate(R.layout.fragment_twitter, container, false)
We would advise creating an extension on ViewGroup. Let's try it:
fun ViewGroup?.inflate(layoutId: Int, attachToRoot: Boolean) = LayoutInflater.from(this?.context).inflate(layoutId, this, attachToRoot)
Three things to highlight. We make the inflate() method null safe by adding ViewGroup?, we utilize the same object to get the context to pass it in two places. Isn't ...
Read now
Unlock full access