0

These are my first steps to create an app for a WearOS watch. I don't unterstand why these little program works for the first and second click. After the third click every click show the right text but after a second it switches back to first text.

I can't see my problem. Any ideas.

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

class WearAppViewModel : ViewModel() {
    private val _text: MutableLiveData<String> = MutableLiveData("Pausiert.")
    val text: LiveData<String> = _text

    fun onTextChange(newText: String) {
        _text.value = newText
    }

    private val _status: MutableLiveData<Int> = MutableLiveData(0)
    val status: LiveData<Int> = _status

    fun onStatusChange(newStatus: Int) {
        _status.value = newStatus
    }
}

@Composable
fun WearAppScreen(wearAppViewModel: WearAppViewModel = WearAppViewModel()) {
    val text: String by wearAppViewModel.text.observeAsState("Pausiert.")
    val status: Int by wearAppViewModel.status.observeAsState(0)
    WearAppContent(
        text = text,
        onTextChange = { wearAppViewModel.onTextChange(it) },
        status = status,
        onStatusChange = { wearAppViewModel.onStatusChange(it) }
    )
}

@Composable
fun WearAppContent(
    text: String,
    onTextChange: (String) -> Unit,
    status: Int,
    onStatusChange: (Int) -> Unit
) {
    Wear2Theme {
        /* If you have enough items in your list, use [ScalingLazyColumn] which is an optimized
         * version of LazyColumn for wear devices with some added features. For more information,
         * see d.android.com/wear/compose.
         */
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colors.background),
            verticalArrangement = Arrangement.Center
        ) {
            Text(
                modifier = Modifier.fillMaxWidth(),
                textAlign = TextAlign.Center,
                color = MaterialTheme.colors.primary,
                text = text
            )
            Button(
                modifier = Modifier
                    .padding(10.dp)
                    .fillMaxWidth(),
                enabled = status == 0,
                onClick = {
                    onTextChange("Läuft.")
                    onStatusChange(1)
                }) {
                Text(
                    text = "Start"
                )
            }
            Button(
                modifier = Modifier
                    .padding(10.dp)
                    .fillMaxWidth(),
                enabled = status == 1,
                onClick = {
                    onTextChange("Pausiert.")
                    onStatusChange(0)
                }) {
                Text(text = "Stop")
            }
        }
    }
}

@Preview(device = Devices.WEAR_OS_SMALL_ROUND, showSystemUi = true)
@Composable
fun DefaultPreview() {
    WearAppScreen()
}

The onTextChange and onStatusChange methods should change the text and enabled the buttons. But what happends after I hit the Start-Button a second time. Why does it disable and then enable after a second?

Update: The problem was the creation of WearAppScreen. I had to put wearAppViewModel in the activity. That solved my problem.

class MainActivity : ComponentActivity() {
    private val wearAppViewModel = WearAppViewModel()

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContent {
            WearAppScreen(wearAppViewModel)
        }
    }
...
@Composable
fun WearAppScreen(wearAppViewModel: WearAppViewModel) {
...

0 Answers0