Tobias Preuss
03/28/2023, 4:21 PMCode example in the thread ...
Landry Norris
03/28/2023, 4:30 PMTobias Preuss
03/28/2023, 5:05 PMandroid:configChanges
when using Compose resp. both XML and Compose across the app?import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import com.example.outlinedtextfieldtest.ui.theme.OutlinedTextFieldTestTheme
class MainActivity : ComponentActivity() {
private var content: Content = Content()
@ExperimentalMaterial3Api
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Screen(content.text) {
content = content.copy(text = it)
}
}
}
}
@ExperimentalMaterial3Api
@Composable
private fun Screen(text: String, onTextChanged: (String) -> Unit) {
OutlinedTextFieldTestTheme {
val focusRequester = remember { FocusRequester() }
var localText by rememberSaveable { mutableStateOf(text) }
OutlinedTextField(
modifier = Modifier.focusRequester(focusRequester),
value = localText,
onValueChange = {
localText = it
onTextChanged(it)
},
)
LaunchedEffect(focusRequester) {
focusRequester.requestFocus()
}
}
}
private data class Content(val text: String = "")
Pablichjenkov
03/28/2023, 5:16 PMLandry Norris
03/28/2023, 5:18 PMStylianos Gakis
03/28/2023, 5:51 PMTobias Preuss
03/28/2023, 6:50 PMStylianos Gakis
03/28/2023, 7:08 PMTobias Preuss
03/28/2023, 8:13 PMephemient
03/29/2023, 5:46 AMTextFieldValue
overload instead of the String
overload, you'll receive and be able to change the cursor/selection position(s)Stylianos Gakis
03/29/2023, 8:18 AMSaver
in its companion object, so you can do rememberSaveable
and this will survive your rotation and activity recreation