使用组合导航开始活动

发布于 2025-02-11 11:20:19 字数 1173 浏览 0 评论 0原文

我正在尝试通过单击底部navbar上的按钮来启动活动。使用navgraphbuilder.navigation()使用composable()调用每个组合屏幕,例如:

navigation(
    startDestination = "home",
    route = "main"
) {
    composable("home") {
       HomeScreen(...)
    }

    // Several more Screens

}

我已经找到了有关navgraphBuilder.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity.Activity. (),所以我在思考类似的事情:

    activity("tickets") {
        this.activityClass = ExternalActivity::class
    }

如果外部活动不需要任何数据,则可以使用。但这确实如此。

想到的唯一可行的替代方案是使用composable()并从那里启动活动:

    composable("tickets") { backStackEntry ->
        val config = // get config from arguments
        context.startActivity(
            Intent(context, ExternalActivity::class.java).apply {
                putExtra("config", config)
            }
        )
    }

或这些行。但这有点混乱,有一些副作用,所以我想避免它。

有什么方法可以使用活动()调用并将数据传递给正在启动的活动?

我受我工作的代码库的体系结构的限制,所以是的,它需要是一项活动(实际上是来自外部库)。

谢谢。

I'm trying to launch an Activity by clicking on a button set on a BottomNavBar. There's a Compose Navigation set up using NavGraphBuilder.navigation() with a composable() call for each Compose screen like:

navigation(
    startDestination = "home",
    route = "main"
) {
    composable("home") {
       HomeScreen(...)
    }

    // Several more Screens

}

I've found out about NavGraphBuilder.activity(), so I was thinking something like:

    activity("tickets") {
        this.activityClass = ExternalActivity::class
    }

And it works, if ExternalActivity doens't need any data to be passed to it. But it does.

The only viable alternative that comes to mind is using a composable() and launching the activity from there:

    composable("tickets") { backStackEntry ->
        val config = // get config from arguments
        context.startActivity(
            Intent(context, ExternalActivity::class.java).apply {
                putExtra("config", config)
            }
        )
    }

Or something along those lines. But it's kind of messy and has some side effects, so I'd like to avoid it.

Is there any way to use the activity() call and pass data to the Activity being launched?

I'm limited by the architecture of the codebase in which I'm working, so yes, it needs to be an Activity (it's actually from an external library).

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挽容 2025-02-18 11:20:19

您必须提供额外的参数:

activity("tickets/{EXTRA_NAME}") {
    argument("EXTRA_NAME") { type = NavType.StringType }
    activityClass = ExternalActivity::class
}
   

然后您以这种方式导航到活动:

navController.navigate("tickets/EXTRA_VALUE")

https://developer.android.com/guide/navigation/design/kotlin-dsl#args

You have to provide the extras as arguments:

activity("tickets/{EXTRA_NAME}") {
    argument("EXTRA_NAME") { type = NavType.StringType }
    activityClass = ExternalActivity::class
}
   

You then navigate to the activity this way:

navController.navigate("tickets/EXTRA_VALUE")

Everything is there: https://developer.android.com/guide/navigation/design/kotlin-dsl#args

烙印 2025-02-18 11:20:19

唯一类似于您要做的事情的唯一一件事是data

activity("tickets") {
    this.activityClass = ExternalActivity::class
    this.data = "Hello World".toUri()    <--
}

[ExternalActivity]
override fun onCreate(savedInstanceState: Bundle?) {
    ...
    val data = intent.data

但是datauri,因此它可能不适合您的需求,尤其是在与外部库打交道时。然后context.startactivity()将是下一个选择,如您的第二种方法。

要注意的一件事是,当您使用context.startactivity()(而不是navgraphbuilder.activity())时,您需要正确设置“当前”目标时活动关闭(例如呼叫navcontroller.navigateup()navcontroller.popbackstack())。如果不是这样,它将跳回您刚刚关闭的活动,因为就NavController而言,您启动的活动(现在关闭)是当前的目的地。

The only thing that remotely resembles what you are trying to do would be data.

activity("tickets") {
    this.activityClass = ExternalActivity::class
    this.data = "Hello World".toUri()    <--
}

[ExternalActivity]
override fun onCreate(savedInstanceState: Bundle?) {
    ...
    val data = intent.data

But data is Uri, so it might not suit your needs, especially if you are dealing with an external library. Then context.startActivity() would be the next choice, as in your second approach.

One thing to note is that when you use context.startActivity() (instead of NavGraphBuilder.activity()), you need to set the "current" destination correctly when the Activity closes (e.g. call navController.navigateUp() or navController.popBackStack()). If not, it will jump back to the Activity you just closed, because as far as NavController is concerned, the Activity you started (and now closed) is the current destination.

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