Android Jetpack Navigation 不会将 onNewIntent/onCreate 意图额外数据更新到目的地
我有一个 NavHost
可组合活动,调用一个简单的可组合函数,并将 TextField
设置为起始目标。有一个 ViewModel
将该文本字段文本 state
存储在 MutableState
对象中。此 Activity 接收 Manifest 中设置的 android.intent.action.SEND
Intent,其 MIME 类型为 “text/plain”
。现在,当我从任何其他应用程序共享文本时,文本字段不会接收文本,但我可以看到 onNewIntent()
回调正在被触发,并且意图 EXTRA_TEXT
正在交付。视图模型文本状态正在使用 EXTRA_TEXT
值进行更新,但是,它不会在 TextField
可组合函数中进行更新。如果我删除 NavHost
,并直接使用 TextField
调用可组合项,则 TextField
文本会正常更新 - 我在这里缺少什么?
class MainActivity : ComponentActivity() {
private val viewModel by viewModels<MainViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NavigationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "Home") {
composable(route = "Home"){
TextShareInput()
}
}
}
}
}
}
// this gets called when I share text data from another app,
// intent is delivered correctly, viewmodel state is updated
// but, the text field does not receive the new state value
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.let { newIntent ->
newIntent.getStringExtra(Intent.EXTRA_TEXT)?.let {
viewModel.editText.value = it
}
}
}
}
@Composable
fun TextShareInput(viewModel: MainViewModel = androidx.lifecycle.viewmodel.compose.viewModel()) {
Column(Modifier.fillMaxSize()) {
TextField(
value = viewModel.editText.value,
onValueChange = { viewModel.editText.value = it })
}
}
class MainViewModel : ViewModel() {
val editText: MutableState<String> = mutableStateOf("")
}
I have an activity with NavHost
composable calling a simple composable function with a TextField
set as start destination. There is a ViewModel
that stores that text field text state
in MutableState<String>
object. This activity receives android.intent.action.SEND
intent set in Manifest with mime type of "text/plain"
. Now, when I share text from any other application, the text field does not receive the text, although, I can see that onNewIntent()
callback is being fired and intent EXTRA_TEXT
is being delivered. The view model text state is getting updated with EXTRA_TEXT
value, however, it's not being updated in the TextField
composable function. If I remove NavHost
, and call the composable with TextField
directly, the TextField
text is updated normally- what am I missing here?
class MainActivity : ComponentActivity() {
private val viewModel by viewModels<MainViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NavigationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "Home") {
composable(route = "Home"){
TextShareInput()
}
}
}
}
}
}
// this gets called when I share text data from another app,
// intent is delivered correctly, viewmodel state is updated
// but, the text field does not receive the new state value
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.let { newIntent ->
newIntent.getStringExtra(Intent.EXTRA_TEXT)?.let {
viewModel.editText.value = it
}
}
}
}
@Composable
fun TextShareInput(viewModel: MainViewModel = androidx.lifecycle.viewmodel.compose.viewModel()) {
Column(Modifier.fillMaxSize()) {
TextField(
value = viewModel.editText.value,
onValueChange = { viewModel.editText.value = it })
}
}
class MainViewModel : ViewModel() {
val editText: MutableState<String> = mutableStateOf("")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将在活动中创建的
viewModel
实例传递给TextShareInput
可组合项。您的 Activity 和 viewModel 具有单独的MainViewModel
实例。所以你有:
You will have to pass the instance of the
viewModel
you are creating in the activity to theTextShareInput
Composable. Your activity and viewModel have separate instances of theMainViewModel
.So you have: