Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ViewModel with Jetpack Compose?

I am trying to use ViewModel with Jetpack Compose by doing a number increment.

It's not working. Maybe I'm not using the ViewModel in the right way.

Here is my Activity code:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Greeting()
        }
    }
}

@Composable
fun Greeting(
    helloViewModel: ViewModel = viewModel()
) {
    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.fillMaxSize()
    ) {
        Text(
            text = helloViewModel.number.toString(),
            fontSize = 60.sp,
            fontWeight = FontWeight.Bold
        )
        Button(onClick = { helloViewModel.addNumber() }) {
            Text(text = "Increment Number ${helloViewModel.number}")
        }

    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    AppTheme {
        Greeting()
    }
}

Here is my ViewModel code:

class ViewModel: ViewModel() {
    var number : Int = 0
    fun addNumber(){
        number++
    }
}

Any ideas what I am doing wrong?

like image 590
Arfin Hosain Avatar asked Dec 16 '25 13:12

Arfin Hosain


2 Answers

Your Composable will not pick up the changes in the ViewModel.number value unless the value is contained within a State object. You can wrap the value in a mutable State property with mutableStateOf() or mutableIntStateOf(), as follows:

class ViewModel: ViewModel() {
    var number : Int by mutableIntStateOf(0)
        private set

    fun addNumber(){
        number++
    }
}

Or you can declare the property as a LiveData or Flow and then reference the value as a State object in your Composable with the LiveData.observeAsState() or Flow.collectAsState extension function.

I suggest you start with the State and Jetpack Compose documentation and the Jetpack Compose: State YouTube video. These two resources explain the basic principles.

like image 145
Philip Dukhov Avatar answered Dec 19 '25 06:12

Philip Dukhov


I use Hilt along with ViewModel. Here is another way of using it

@Composable
fun PokemonListScreen(
    navController: NavController
) {

    val viewModel = hiltViewModel<PokemonListVm>()
    val lazyPokemonItems: LazyPagingItems<PokedexListEntry> = viewModel.pokemonList.collectAsLazyPagingItems()

}

I use this composable instead of a fragment and I keep one reference to the composable in the parent

like image 21
Devrath Avatar answered Dec 19 '25 06:12

Devrath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!