October 2019
Intermediate to advanced
434 pages
11h 54m
English
Kotlin allows us to perform null-safe method calls on variables that may, or may not, be null. One example of how this can reduce boilerplate in our code is in the handling of savedInstanceState.
A common pattern is to check whether savedInstanceState is non-null within onCreate(), and if it is, handle the restoration of that state. We can see an example of this in the following code snippet:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_details) savedInstanceState?.let { it }}fun restoreSavedState(savedInstanceState: Bundle) { // restore state}
Using a null-safe call to the let() function, we can safely restore our state only if savedInstanceState ...
Read now
Unlock full access