I have some problem with lifecycle… When i click ...
# compose
a
I have some problem with lifecycle… When i click button CHANGE title doesn/t change.. and i think.. code is correct.. or not?
🧵 4
when I turn off the display … after some seconds.. Text change string… it’s crazy
c
Please move your code blocks into the thread, that’s why people are reacting with 🧵
a
Copy code
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {

            val navController = rememberNavController()
            MyAppTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    NavigationComponent(navController)
                }
            }
        }
    }
}
Copy code
@Composable
fun NavigationComponent(navController: NavHostController) {
    NavHost(
        navController = navController,
        startDestination = Destination.Dashboard.path
    ) {
        composable(Destination.Dashboard.path) {
            val viewModel: DashBoardViewModel = hiltViewModel()
            DashBoardScreen(viewModel)
        }

    }
}

@Composable
fun DashBoardScreen(viewModel: DashBoardViewModel) {

    val dashBoardState by viewModel.uiState.collectAsState()


    Column(modifier = Modifier.fillMaxSize().background(Color.White)) {
        Text(
            text = "${dashBoardState.title}",
            fontSize = 20.sp,
            color = Color.Black,
            textAlign = TextAlign.Center,
            modifier = Modifier.padding(16.dp)
        )

        Button(onClick = {viewModel.changeTitle()}) {
            Text(
                text = "CHANGE")
        }
    }
}

@HiltViewModel
class DashBoardViewModel @Inject constructor(
) : ViewModel() {


    val uiState = MutableStateFlow(DashboardState(false))


    fun changeTitle() {
        viewModelScope.launch {

            uiState.emit(uiState.value.copy(
                title = "Ciao"

            ))
        }
    }
}

data class DashboardState(
    var isLoading: Boolean = false,
    var title: String = "start"
)
I created a project… https://github.com/GioeleDev95/stateexaple2 it doesn’t work
ö
I tried your code and everything is working. You can change
text = "${dashBoardState.title}"
this to
text = dashBoardState.title
. Because it’s already string. And maybe some little improvements for SOLID but apart from that your code is working.
a
i tried in two smartphone and when tap CHANGE Text has same text “start” and not ciao
ok i understood, if i use “androidx.compose.uiui1.3.1” i have the bug… ui doesn’t change
Copy code
"androidx.compose.ui:ui:1.3.1" si mandatory to use compile and target 33
ok when i use “androidx.compose.uiui1.3.1” i have bug but with 1.20 works