State in Widget — Jetpack Glance

AndroidAI
3 min readNov 5, 2023

I think main concept or heart of Jetpack compose is State. Using which we will change the UI according to condition or different data.

Same applies for Jetpack Glance where we can use the State concept to design or redesign the Widget UI. So let us see how we can do that here.

State

In our app, there will be a scenario where we need to show a UI to user according to behaviour changes instantly. For example, when we call a API call we will show the loading , then after we gets data we will show UI according to Success or Failure. So here the UI changes according to the behaviour and correct UI.

To solve the issues we will have a state handling in Jetpack compose which will create a recomposition for composable functions. So is this feature available in Jetpack Glance?

State types In Glance

Jetpack Glance provides a two way to handle the UI state

  1. MutableState- Similar to State in Jetpack compose
  2. Glance State — a Persisitent storage

Mutable State

Here is the example of mutable state usage in Jetpack Glance. In our example we have used a simple check box and handle the checked or unchecked using state.

--

--