Android : Kotlin : Composable Button

Bonjour tout le monde,
D’avance excusez moi si je ne suis pas au bon endroit. C’est ma première fois.

J’apprend tout seul a développer sur Android Studio sur Kotlin

Je souhaite faire une application (débutant) qui me permette de faire plus 1 et appeler une fonction.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
@Composable fun afficheContenu(etape: setStepAppVal, img: Painter, name: String, rmk: String, modifier: Modifier){ Column( modifier = Modifier .fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ) { Button( onClick = { nextStepApp(etape, modifier) } ) { Image( painter = img, contentDescription = name, modifier = Modifier .size(200.dp) ) } Spacer(modifier = Modifier.height(16.dp)) Text( text = rmk ) } } @Composable fun nextStepApp(etape: setStepAppVal, modifier: Modifier){ etape.modifierVal(etape.value+1) stepApp(etape, modifier) }

Quand

Code : Sélectionner tout - Visualiser dans une fenêtre à part

onClick = { nextStepApp(etape, modifier) }

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 ?

Merci de votre aide.
Bonne journée

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?

Thank you for your help.
Good day

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.

1 Like