I have a method 'activity' which hosts a 'screen' and since state hoisting is cool, I did just that but with a problem.
While the code works perfectly (incrementing the value) using Modifier.clickable, the same code does not properly work using detectTapGestures::onTap as can be observed through Log.d("ExampleScreen", "onClick Multi Count is $multiCount")
@Composable
fun ExampleActivity() {
var multiCount by remember {
mutableStateOf(0)
}
Log.d("ExampleActivity", "Multi Count is $multiCount") //this works either way
ExampleScreen(
multiCount = multiCount,
incrementMultiCount = {
multiCount = ++multiCount
}
)
}
@Composable
fun ExampleScreen(
modifier: Modifier = Modifier,
multiCount: Int,
incrementMultiCount: () -> Unit
) {
Log.d("ExampleScreen", "Example Multi Count is $multiCount") //this works either way
Column(
modifier = modifier
.fillMaxSize()
.padding()
) {
Text(
modifier = Modifier
.size(100.dp, 50.dp)
.background(MaterialTheme.colorScheme.primaryContainer)
.clickable {//this works
incrementMultiCount()
Log.d("ExampleScreen", "onClick Multi Count is $multiCount")
}
/* .pointerInput(Unit) { //this does not work
detectTapGestures(
onTap = {
incrementMultiCount()
Log.d("ExampleScreen", "onClick Multi Count is $multiCount") //this stays 0
},
onLongPress = {
}
)
}*/, text = "Click to confirm"
)
}
}
Partially, it works the same (incrementing works but not reading the value) as shown in logs defined in ExampleActivity and function body of ExampleScreen but doesn't in the onTapGesture.
If the remember value is directly in the ExampleScreen composable, onTap and clickable works perfectly but not what I wanted.
Finally, before suggesting I use what works, I wanted to use detectTapGestures because I really need the LongPress method for a secondary work.
Plus I will really appreciate an explanation since I thought both works the same.