My UI will not update when using a class property in Jetpack Compose

I am new to both Kotlin and Compose. Using some online guides I have been able to create some very simple UIs. I now have a problem that one of my UIs will not recompose when I use a property of a class within a composable. Below is my code. I create a class that has an INT property which is initialized to 3 and a method that will increase the property’s value. I am using the property value in the text of a button.

class Cards {
    var number: Int = 3

    fun incNum() {
        number++
    }
}

@Composable
fun TestApp() {
    val myCard by remember { mutableStateOf(Cards()) }

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier.fillMaxSize(),
    ) {
        Button(onClick = {
            myCard.incNum()
        }) {
            Text(
                text = "Clicks: ${myCard.number}",
                fontSize = 50.sp,
            )
        }
    }
}

When I click the button the value of the property increases but the new value does not display in the text of the button. I have used the debugger and verified that the new value of the property is maintained between clicks (3, 4, 5, etc.) but no recomposition occurs.

I don’t understand why the UI will not update. Any help with this will be greatly appreciated. Let me know if you need more information about the problem.

The issue you’re experiencing is because compose doesn’t automatically observe changes in regular class properties. Compose only observes changes in state objects, which are wrapped inside MutableState. In your code, you’re wrapping the Cards class in a state object, but changes to the number property inside Cards are not detected because the Cards reference itself doesn’t change.

To make Compose aware of changes to the number property, you need to wrap it inside a MutableState object. This way, compose will recompose the UI whenever the value changes.

Here’s how you can modify your code:

class Cards {
    //var number: Int = 3
    var number: MutableState<Int> = mutableStateOf(3)


    fun incNum() {
        number.value++
    }
}


@Composable
fun TestApp() {
    val myCard by remember { mutableStateOf(Cards()) }

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier.fillMaxSize()
    ) {
        Button(onClick = {
            myCard.incNum()
        }) {
            Text(
                text = "Clicks: ${myCard.number.value}",
                fontSize = 50.sp
            )
        }
    }
}

Another commonly used solution is to declare your Cards class as an immutable data class. This way, you cannot change its properties directly, since changing properties directly does not trigger recomposition anyway, this approach prevents such changes. Instead, whenever you need to change a property, you create a new object with the updated data using the copy method of the data class. This changes the reference of the class, which causes compose to recompose.

Here’s an example:

data class Cards(
    val number: Int = 3
)


@Composable
fun TestApp() {
    var myCard by remember { mutableStateOf(Cards()) }

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier.fillMaxSize()
    ) {
        Button(onClick = {
            myCard = myCard.copy(number = myCard.number + 1) // create new object that hold the new data
        }) {
            Text(
                text = "Clicks: ${myCard.number}",
                fontSize = 50.sp
            )
        }
    }
}

@Abdo21’s answer is correct, I just want to add another solution to the problem: Make Cards an immutable data class.

data class Cards(
    val number: Int = 3,
)

number is a val now so it can never change. This makes Cards immutable and therefore the incNum function isn’t needed anymore. Instead you would create a new object when you want to increase number. Your Composable would then look like this:

@Composable
fun TestApp() {
    var myCard by remember { mutableStateOf(Cards()) }

    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier.fillMaxSize(),
    ) {
        Button(onClick = {
            myCard = myCard.copy(number = myCard.number + 1)
        }) {
            Text(
                text = "Clicks: ${myCard.number}",
                fontSize = 50.sp,
            )
        }
    }
}

Creating a new instance each time may seem a bit excessive, but it is quite performant and solves a lot of problems around Compose functions that work best (or even at all) when objects do not mutate (except when explicitly wrapped in a MutableState). Immutability is also the recommended way to go when you use a view model to store your state.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật