Rene GensAuthorBlogUsesNotesGitHubLinkedIn

Idiomatic if with kotlin

07 March, 2018 - 1 min read

Here is a safe and idiomatic way to the null safe Kotlin operator to check against a value and execute some code.

someObject?.takeIf{ it.status }?.apply{ doThis() }

And here is a real word example:

item?.takeIf { it.itemId == android.R.id.home }?.run {             
    onBackPressed()         
}

Check some other Android posts on the blog, I hope they will help.

© 2020, Built with Gatsby