Android Jetpack Navigation 不会将 onNewIntent/onCreate 意图额外数据更新到目的地

发布于 2025-01-14 00:49:00 字数 2360 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2025-01-21 00:49:00

您必须将在活动中创建的 viewModel 实例传递给 TextShareInput 可组合项。您的 Activity 和 viewModel 具有单独的 MainViewModel 实例。

所以你有:

composable(route = "Home"){
  TextShareInput(viewModel)
}

You will have to pass the instance of the viewModel you are creating in the activity to the TextShareInput Composable. Your activity and viewModel have separate instances of the MainViewModel.

So you have:

composable(route = "Home"){
  TextShareInput(viewModel)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文