il ne veut pas que j’appelle une function @Composable, mais si j’enlève @Composable à la function “fun nextStepApp”, il me dit de le mettre ?
Du coup le serpent se mort la queue !
Depuis le onClick() j’ai besoin de changer une valeur puis d’appeler une function.
Comment faire ?
Hello everyone,
Apologies in advance if I’m not in the right place. It’s my first time.
I’m teaching myself to develop on Android Studio on Kotlin
I want to make an application (beginner) that allows me to do plus 1 and call a function.
When
Code: Select all - View in a separate window
onClick = { nextStepApp(step, modify) }
it doesn’t want me to call a @Composable function, but if I remove @Composable from the “fun nextStepApp” function, it tells me to put it ?
Suddenly the snake died its tail!
From onClick() I need to change a value then call a function.
How to do it?
As there is no other answer, I will give it a shot, even though I’m learning Android and Compose myself. I can’t really say something about the behavior you see, but I think it won’t work the way you try. Composable functions have to be stateless, as they can be interrupted at any point for all kinds of reasons (like turning the phone), or even disposed.
You need a “safe place” outside of the Composable function tree to put your data, and you have to make sure that the application is aware of the changes and doesn’t lose it. The safe place can be your activity, in more advanced applications you often see viewmodels. There are several ways to make sure that data are really stored, e.g. the remember function. And to make sure that the application is notified about changes and updates the UI accordingly, there are also a lot of ways, the most basic being to wrap it in a MutableState (but there are also flows etc).
Here is the easiest example I could find for a Android Compose app that changes a value and displays the results:
Note that the value needed for the UI (here count) is passed as a normal argument to the Composable function, but is not directly modified. This is done by getting also a lambda argument passed for onClick, which was prepared in the MainActivity, where you have access to these data. I think you can do the same with your nextStepApp call.