After I click login once (and it fails for example...
# compose
j
After I click login once (and it fails for example), if I later change the value of email or password, the code in
onClick
gets called every time without a click on the button 🤷🏾
Copy code
var userEmail by remember { mutableStateOf("") }
var userPassword by remember { mutableStateOf("") }
Copy code
Button(
    text = "Log in",
    onClick = {
        viewModel.login(userEmail, userPassword)
    }
)
a
are you clearing the fields after a failed attempt @Jeff?
a
do you have a minimal repro case?
the code you posted alone does not have the behavior you're describing in a clean project
(and if it did, a great many things would be very, very broken 🙂 )
as that's not a standard material
Button
can you post your
Button
composable?
j
No. @Alexander Suraphel I'm not. Why should I do that? My assumption is when I make changes to either email or password, the state will get updated and the data will be submitted only when I click the button again.
😅 @Adam Powell Okay. I guess there must be something I'm doing wrong. Or I haven't wrapped my head fully around the concept of state in compose. Here's my button.
Copy code
@Composable
fun SmartButton(modifier: Modifier = Modifier, text: String, enabled: Boolean = true, onClick: () -> Unit = {}) {
    Box(modifier = modifier, contentAlignment = Alignment.Center) {
        Button(
            modifier = Modifier
                .fillMaxWidth()
                .height(50.dp),
            shape = MaterialTheme.shapes.medium,
            enabled = enabled,
            onClick = onClick,
            colors = ButtonDefaults.buttonColors(
                backgroundColor = MaterialTheme.colors.secondary,
                contentColor = MaterialTheme.colors.onSecondary
            )
        ) {
            Text(text = text, style = MaterialTheme.typography.button)
        }
    }
}
a
there's not anywhere else
onClick
might be called during composition, is there?
j
Nope
a
got a stack trace of one of those phantom follow-up calls to see where it's being invoked from?
j
My state 😱
a
try
Throwable().printStackTrace()
in that onClick block right before or after the viewModel.login call
👍 1
j
2021-07-19 07:58:05.303 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err: java.lang.Throwable
2021-07-19 07:58:05.306 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at com.smartapplicationsgroup.pae.androidApp.compose.LogInScreenKt$LogInScreen$2$1$3.invoke(LogInScreen.kt:173)
2021-07-19 07:58:05.307 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at com.smartapplicationsgroup.pae.androidApp.compose.LogInScreenKt$LogInScreen$2$1$3.invoke(LogInScreen.kt:166)
2021-07-19 07:58:05.308 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke-k-4lQ0M(Clickable.kt:137)
2021-07-19 07:58:05.309 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke(Clickable.kt:137)
2021-07-19 07:58:05.311 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1$1.invokeSuspend(TapGestureDetector.kt:378)
2021-07-19 07:58:05.312 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
2021-07-19 07:58:05.314 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:178)
2021-07-19 07:58:05.315 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:166)
2021-07-19 07:58:05.316 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:398)
2021-07-19 07:58:05.317 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:432)
2021-07-19 07:58:05.318 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:421)
2021-07-19 07:58:05.320 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:329)
2021-07-19 07:58:05.322 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:432)
2021-07-19 07:58:05.324 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.dispatchPointerEvent(SuspendingPointerInputFilter.kt:330)
2021-07-19 07:58:05.325 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:343)
2021-07-19 07:58:05.326 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:287)
2021-07-19 07:58:05.328 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:275)
2021-07-19 07:58:05.329 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:151)
2021-07-19 07:58:05.330 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:90)
2021-07-19 07:58:05.332 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-gBdvCQM(PointerInputEventProcessor.kt:77)
2021-07-19 07:58:05.333 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:860)
2021-07-19 07:58:05.334 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3118)
2021-07-19 07:58:05.336 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2799)
2021-07-19 07:58:05.336 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3118)
2021-07-19 07:58:05.337 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2799)
2021-07-19 07:58:05.337 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3118)
2021-07-19 07:58:05.338 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2799)
2021-07-19 07:58:05.339 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3118)
2021-07-19 07:58:05.339 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2799)
2021-07-19 07:58:05.339 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:488)
2021-07-19 07:58:05.340 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
2021-07-19 07:58:05.340 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.app.Activity.dispatchTouchEvent(Activity.java:4125)
2021-07-19 07:58:05.341 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:446)
2021-07-19 07:58:05.342 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.View.dispatchPointerEvent(View.java:14568)
2021-07-19 07:58:05.343 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6016)
2021-07-19 07:58:05.344 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5819)
2021-07-19 07:58:05.344 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5310)
2021-07-19 07:58:05.344 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5367)
2021-07-19 07:58:05.345 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5333)
2021-07-19 07:58:05.345 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5485)
2021-07-19 07:58:05.345 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5341)
2021-07-19 07:58:05.345 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5542)
2021-07-19 07:58:05.346 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5314)
2021-07-19 07:58:05.346 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5367)
2021-07-19 07:58:05.346 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5333)
2021-07-19 07:58:05.347 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5341)
2021-07-19 07:58:05.348 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5314)
2021-07-19 07:58:05.348 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8080)
2021-07-19 07:58:05.348 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8031)
2021-07-19 07:58:05.349 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7992)
2021-07-19 07:58:05.349 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8203)
2021-07-19 07:58:05.349 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)
2021-07-19 07:58:05.349 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.os.MessageQueue.nativePollOnce(Native Method)
2021-07-19 07:58:05.350 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.os.MessageQueue.next(MessageQueue.java:335)
2021-07-19 07:58:05.350 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.os.Looper.loop(Looper.java:183)
2021-07-19 07:58:05.351 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7656)
2021-07-19 07:58:05.351 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-07-19 07:58:05.352 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2021-07-19 07:58:05.352 23111-23111/com.smartapplicationsgroup.pae.androidApp.dev.debug W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
The culprit is not the button. The onClick is called only when the button is clicked. Same to
viewModel.login
My state seems to be changing in a funny way. Every time I change the value of email and password after a failed login.
Copy code
when (state) {
    is DataState.Success -> {
        navigateToMainFromLogin()
    }
    is DataState.Error -> {
        coroutineScope.launch {
            snackbarHostState.showSnackbar(message = (state as DataState.Error).exception)
        }
    }
    else -> {
    }
}