Has anyone encountered an issue with Jetpack Compo...
# compose
s
Has anyone encountered an issue with Jetpack Compose text fields (OutlinedTextField) on Samsung devices? java.lang.StringIndexOutOfBoundsException 🧵
Copy code
@Composable
fun CommentContentLayout(
    state: TextFieldState,
    onTextEntered: (String) -> Unit,
) {
    OutlinedTextField(
        state = state,
        modifier = Modifier.fillMaxWidth(),
        readOnly = false,
        lineLimits = TextFieldLineLimits.MultiLine(3),
    )
    LaunchedEffect(state) {
        snapshotFlow { state.text.toString() }
            .collectLatest { newText ->
                onTextEntered(newText)
            }
    }
}
The state is created like this:
Copy code
kotlin
val state = rememberTextFieldState()
For edit mode, it is initialized with existing content:
Copy code
kotlin
val state = rememberTextFieldState(initialText)
```
- Jetpack Compose Material3
OutlinedTextField
- State-based API:
TextFieldState
- Multiline field - Only reported on Samsung devices / Samsung Keyboard - Crash appears related to IME batch edit / delete operation - Example crash has
length=56; index=56
, so it is not limited to very long text
Copy code
Form Crash report: 
Fatal Exception: java.lang.StringIndexOutOfBoundsException: length=56; index=56
    at java.lang.String.charAt(String.java)
    at androidx.compose.foundation.text.input.internal.ToCharArray_androidKt.toCharArray(ToCharArray.android.kt:32)
    at androidx.compose.foundation.text.input.internal.PartialGapBuffer.replace(GapBuffer.kt:246)
    at androidx.compose.foundation.text.input.internal.PartialGapBuffer.replace(GapBuffer.kt:280)
    at androidx.compose.foundation.text.input.TextFieldBuffer.replace$foundation(TextFieldBuffer.kt:313)
    at androidx.compose.foundation.text.input.TextFieldBuffer.replace(TextFieldBuffer.kt:285)
    at androidx.compose.foundation.text.input.TextFieldBufferKt.delete(TextFieldBuffer.kt:646)
    at androidx.compose.foundation.text.input.internal.ImeEditCommand_androidKt.imeDelete(ImeEditCommand.android.kt:473)
    at androidx.compose.foundation.text.input.internal.ImeEditCommand_androidKt.deleteSurroundingText$lambda$0(ImeEditCommand.android.kt:301)
    at androidx.compose.foundation.text.input.internal.DefaultImeEditCommandScope.endBatchEdit(ImeEditCommand.android.kt:132)
    at androidx.compose.foundation.text.input.internal.StatelessInputConnection.endBatchEdit(StatelessInputConnection.android.kt:218)
h
Which compose version are you using? I'm sure we have fixed this problem relatively recently but can't tell right now which version had the fix.
The reports were almost exclusively coming from Samsung devices due to Samsung Keyboard but I've seen this happen with Swiftkey a couple of times.
s
@Halil Ozercan Thanks! Indeed, the last time I updated Compose and the other dependencies was around three months ago. I'll try updating everything to the latest versions. But it's quite interesting that I couldn't find any complaints about it. Kind of interesting.🙏