Jetpack Compose 和 Compose Navigation 如何处理 Android 活动? [英] How are Android activities handled with Jetpack Compose and Compose Navigation?

查看:99
本文介绍了Jetpack Compose 和 Compose Navigation 如何处理 Android 活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究 Jetpack Compose,以尝试使用现代 Android 架构组件构建功能丰富的应用程序.传统上,我的应用程序中的每个屏幕(或导航单元)要么是一个 Activity 要么是一个片段,每个都有自己的生命周期绑定,但使用 Jetpack Compose 和 Compose Navigation 库,我会做这样的事情:

I'm currently studying Jetpack Compose in an attempt to build a feature-rich application using modern Android architecture components. Traditionally, each screen (or navigation unit) in my application would be either an activity or a fragment, each with its own lifecycle bindings, but with Jetpack Compose and the Compose Navigation library, I would do something like this:

MainActivity.kt:

MainActivity.kt:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()

            NavHost(navController = navController, startDestination = "main") {
                composable("main") { MainScreen(navController) }
                // More composable calls
            }
        }
    }
}

其中 MainScreen 只是一个可组合的.我的问题是:

Where MainScreen is just a composable. My questions are:

  • 这里的生命周期"等价物是什么?对于这个可组合?假设我想在屏幕加载、销毁等时执行一些操作.这可能与我有更多屏幕和它们之间导航的情况更相关
  • 是否可以通过某种方式在 Compose 和标准活动之间进行集成?也就是说,像您一样为屏幕定义活动,每个活动都是一个 ComponentActivity 并定义自己的可组合布局?是否出于某种原因不鼓励这样做?
  • What is the equivalent here of a "lifecycle" for this composable? Say I want to perform some action when the screen is loaded, when it is destroyed etc. This is perhaps more relevant to the case where I have more screens and navigation between them
  • Is there some way to integrate between Compose and standard activities? That is, define activities for screens as you would, with each activity being a ComponentActivity and defining its own composable layout? Is this discouraged for some reason?

推荐答案

Compose 应用程序旨在用于没有片段的单活动架构.

The Compose application is designed to be used in a single-activity architecture with no fragments.

您仍然可以拥有多个活动或片段,并在每个活动或片段中使用 setContent,但在这种情况下,活动之间的数据传输就落在了您的肩上.如果您要向以旧方式构建的现有应用程序添加新的 Compose 屏幕,请使用此方法.

You can still have multiple activities or fragments and use setContent in each of them, but in this case the transfer of data between activities falls on your shoulders. Use this approach if you're adding new Compose screens to an existing application built the old way.

但是有了 Compose,使用 Compose Navigation 在单个活动中完成所有导航要容易得多.更少的代码,由于没有不必要的代码层而获得更好的性能,易于传输数据等.

But with Compose, it's much easier to do all the navigation within a single activity using Compose Navigation. Much less code, better performance due to no unnecessary code layers, easy to transfer data, etc.

要使用视图生命周期,请查看撰写副作用:

To work with the view lifecycle, check out compose side-effects:

  1. LaunchedEffect 可用于当视图出现时执行一个动作.它还在绑定到当前可组合的协程上下文中运行:您可以轻松运行挂起函数,并且当视图从视图层次结构中消失时 - 协程将被取消.
  2. DisposableEffect 可用于订阅/取消订阅回调.

当您旋转屏幕时,无论您按哪个键,所有效果都会重新启动.

@Composable
fun MainScreen(navController: NavController) {
    LaunchedEffect(Unit) {
        println("LaunchedEffect: entered main")
        var i = 0
        // Just an example of coroutines usage
        // don't use this way to track screen disappearance
        // DisposableEffect is better for this
        try {
            while (true) {
                delay(1000)
                println("LaunchedEffect: ${i++} sec passed")
            }
        } catch (cancel: CancellationException) {
            println("LaunchedEffect: job cancelled")
        }
    }
    DisposableEffect(Unit) {
        println("DisposableEffect: entered main")
        onDispose {
            println("DisposableEffect: exited main")
        }
    }
}

还要注意,在这两种情况下,以及在 compose 中的许多其他情况下,您都将 key 传递给这些函数.这有助于撰写理解何时应该重新计算值.在我的示例中,它是 Unit,这意味着它在视图消失之前不会改变.但是如果你创建一个 remember 值,使用视图模型中的另一个动态值,或者将另一个参数传递给可组合,你可以将它作为 key 传递,这将取消当前 LaunchedEffect 作业并为 DisposableEffect 调用 onDispose,您的作业将使用更新的 key 值重新启动.您可以根据需要传递任意数量的密钥.

Also note that in both cases, and in many other cases in compose, you pass key to these functions. This helps compose understand when the value should be recomputed. In my example it is Unit, which means that it won't change until the view is gone. But if you create a remember value, use another dynamic value from the view model, or pass another argument to composable, you can pass it as a key, this will cancel the current LaunchedEffect job and call onDispose for DisposableEffect, and your job will be restarted with the updated key value. You can pass as many keys as you want.

文档中阅读有关 Compose 状态的更多信息.

Read more about the state in Compose in documentation.

这篇关于Jetpack Compose 和 Compose Navigation 如何处理 Android 活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆