'super.Composable function crashes at text input

App crashes when trying to type in TextField that is in superclass

(video: https://imgur.com/a/6RNc2mU)

Exception:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testcompose, PID: 28928
java.lang.ArrayIndexOutOfBoundsException: src.length=16 srcPos=3 dst.length=16 dstPos=2 length=-2
    at java.lang.System.arraycopy(Native Method)
    at kotlin.collections.ArraysKt___ArraysJvmKt.copyInto(_ArraysJvm.kt:1247)
    at androidx.compose.runtime.collection.MutableVector.removeAt(MutableVector.kt:794)
    at androidx.compose.ui.node.LayoutNode.removeAt$ui_release(LayoutNode.kt:250)
    at androidx.compose.ui.node.UiApplier.remove(UiApplier.android.kt:36)
    at androidx.compose.runtime.ComposerImpl$realizeMovement$1.invoke(Composer.kt:2903)
    at androidx.compose.runtime.ComposerImpl$realizeMovement$1.invoke(Composer.kt:2903)
    at androidx.compose.runtime.CompositionImpl.applyChanges(Composition.kt:629)
    at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:479)
    at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:416)
    at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34)
    at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
    at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
    at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1074)
    at android.view.Choreographer.doCallbacks(Choreographer.java:897)
    at android.view.Choreographer.doFrame(Choreographer.java:822)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1061)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:8056)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

Sample project:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val foo = ChildFoo()
        setContent {
            TestComposeTheme {
                Column {
                    foo.ShowTextField()
                }
            }
        }
    }
}

open class Foo {
    var value1 by mutableStateOf("123")
    @Composable
    open fun ShowTextField(){
        OutlinedTextField(value = value1, onValueChange = { newValue: String -> value1 = newValue })
    }
}

class ChildFoo : Foo() {
    var value2 by mutableStateOf("456")
    @Composable
    override fun ShowTextField(){
        super.ShowTextField()
        OutlinedTextField(value = value2, onValueChange = { newValue: String -> value2 = newValue })
    }
}

But if I move Foo class completely to ChildFoo, it works fine:

class ChildFoo {
    var value1 by mutableStateOf("123")
    var value2 by mutableStateOf("456")
    @Composable
    fun ShowTextField(){
        OutlinedTextField(value = value1, onValueChange = { newValue: String -> value1 = newValue })
        OutlinedTextField(value = value2, onValueChange = { newValue: String -> value2 = newValue })
    }
}

Tried with (kotlin 1.5.21 and compose 1.0.1) and (koltin 1.6.10 and compose 1.1.1).

Looks like it is a bug of jetpack compose or am I missing something?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source