How to convert String to Int in Kotlin?

You could call toInt() on your String instances: fun main(args: Array<String>) { for (str in args) { try { val parsedInt = str.toInt() println(“The parsed int is $parsedInt”) } catch (nfe: NumberFormatException) { // not a valid int } } } Or toIntOrNull() as an alternative: for (str in args) { val parsedInt = str.toIntOrNull() … Read more

IntArray vs Array in Kotlin

Array<Int> is an Integer[] under the hood, while IntArray is an int[]. That’s it. This means that when you put an Int in an Array<Int>, it will always be boxed (specifically, with an Integer.valueOf() call). In the case of IntArray, no boxing will occur, because it translates to a Java primitive array. Other than the … Read more

How to make “inappropriate blocking method call” appropriate?

The warning is about methods that block current thread and coroutine cannot be properly suspended. This way, you lose all benefits of coroutines and downgrade to one job per thread again. Each case should be handled in a different way. For suspendable http calls you can use ktor http client. But sometimes there is no … Read more

How can I detect a click with the view behind a Jetpack Compose Button?

When button finds tap event, it marks it as consumed, which prevents other views from receiving it. This is done with consumeDownChange(), you can see detectTapAndPress method where this is done with Button here To override the default behaviour, you had to reimplement some of gesture tracking. List of changes comparing to system detectTapAndPress: I … Read more

Override getter for Kotlin data class

After spending almost a full year of writing Kotlin daily I’ve found that attempting to override data classes like this is a bad practice. There are 3 valid approaches to this, and after I present them, I’ll explain why the approach other answers have suggested is bad. Have your business logic that creates the data … Read more

Why does mutableStateOf without remember work sometimes?

It’s a feature of Compose about scoping and smart recomposition. You can check my detailed answer here. What really makes your whole Composable to recompose is Column having inline keyword. @Composable inline fun Column( modifier: Modifier = Modifier, verticalArrangement: Arrangement.Vertical = Arrangement.Top, horizontalAlignment: Alignment.Horizontal = Alignment.Start, content: @Composable ColumnScope.() -> Unit ) { val measurePolicy … Read more

Kotlin static methods and variables

The closest thing to Java’s static fields is a companion object. You can find the documentation reference for them here: https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects Your code in Kotlin would look something like this: class Foo { companion object { lateinit var instance: Foo } init { instance = this } } If you want your fields/methods to be … Read more

tech