一个完整的非平凡的 Jetpack Compose 支持的应用程序,只有很少(一个?)活动? [英] A whole non-trivial Jetpack Compose-backed app with very few (one?) activities?

查看:50
本文介绍了一个完整的非平凡的 Jetpack Compose 支持的应用程序,只有很少(一个?)活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Jetpack Compose,是否是一种可行的方式来开发应用程序,只需要一个 Activity,并让 Compose 管理它们之间的导航?@Composables?什么时候需要添加更多 ActivityFragments?

长版:几年前我做过一些 Android 开发,你会在其中使用 Activities,并且有必要在任何非平凡的应用程序中拥有多个它们,因为它们是原始"建筑每个应用程序的块.然后,Fragments 出现了,可以在单个 Activity 中托管不同的片段(它们支持替换等,所有这些都带有动画),突然间可以用更少的 Activity 实现相同的应用程序.>

多年后,我又回到了 Android 开发领域,这次是使用 Jetpack Compose.我已经使用 Jetpack Compose Navigation(https://developer.android.com/jetpack/compose/navigation),其中 @Composables 被替换,我很高兴一切都变得简单了.似乎现在可以拥有一个只有一个活动的应用程序,也可能没有片段.我认为有时可能需要不同的活动,例如如果应用程序可以处理各种Intents,但即便如此,Compose Navigation 也支持深度链接等内容.

解决方案

使用 Jetpack Compose,是否有一种可行的方法可以通过仅具有单个 Activity 来开发应用程序,并让 Compose 管理@Composables 之间的导航?

这就是理论.对于大型应用程序(想想 Facebook)还有待证明,但对于中小型应用程序,据我们所知,它应该没问题.

另请注意,单 Activity 架构模式对于 Compose UI 来说并不陌生——一些开发人员一直在走这条路线,将片段用于屏幕.

<块引用>

何时需要添加更多活动或片段?

假设我们没有遇到一些实际限制,需要其他活动和/或片段的主要场景是与框架类或第三方库的集成,这些外部依赖项需要活动或片段.

例如,应用小部件可以具有配置活动.您可能也可以让您的一次性活动处理这种情况,但您可能会发现为应用程序小部件配置设置一次性的小型活动类会更简单.这样,您的主 Activity 类就不会被试图区分正常启动和应用程序小部件配置启动的逻辑所困扰.更一般地说,Activity 会成为您 UI 的入口点,您可能只需要为不同角色设置不同入口点的 Activity 数量即可.

而且,总体而言,一切适度".如果将某些内容作为单独的 Activity 或片段来实现,将对您的应用程序有实质性的帮助 - 易于初始开发,易于维护等 - 使用它!仅仅因为很少活动是可行的,并不意味着多于一项活动是不好的.我们现在有了更简单的方法来不强制我们可能不需要的多个活动.

With Jetpack Compose, is it a viable way to develop an app by having a single Activity only, and have Compose manage navigation between @Composables? When would it be necessary to add more Activities or Fragments?

Long version: I did some Android development years ago, where you would work with Activities and it was necessary to have multiple of them in any non-trivial app as they were the 'primitive' building blocks of every app. Then, Fragments came along and it was possible to host different ones within a single activity (they supported replacement etc. all with animations) and suddenly it was possible to implement the same app with fewer activities.

Years later, I'm back to Android development, this time with Jetpack Compose. I have already written an app with a couple of screens, using Jetpack Compose Navigation (https://developer.android.com/jetpack/compose/navigation), where @Composables are replaced and I'm very happy with by how much simpler everything has become. It seems that it is now possible to have an app with just a single activity, possibly without fragments either. I think that sometimes different activities might be necessary, e.g. if the application can process various Intents, but even then Compose Navigation supports stuff like deep linking etc.

解决方案

With Jetpack Compose, is it a viable way to develop an app by having a single Activity only, and have Compose manage navigation between @Composables?

That is the theory. It remains to be proven for massive apps (think Facebook), but for small to mid-size apps, as far as we know, it should be fine.

Also note that the single-activity architecture pattern is not new to Compose UI — some developers have been going that route with fragments for the screens.

When would it be necessary to add more Activities or Fragments?

Assuming that we do not encounter some practical limit, the primary scenario for needing other activities and/or fragments would be for integration with framework classes or third-party libraries, where those external dependencies expect activities or fragments.

For example, app widgets can have a configuration activity. It is possible you could have your one-and-only activity handle that case too, but you might find it simpler to have a one-off small activity class just for the app widget configuration. That way, your main activity class does not get cluttered with logic for trying to distinguish between normal launches and app widget configuration launches. More generally, activities become entry points into your UI, and you probably only need as many activities as you want distinct entry points for different roles.

And, overall, "everything in moderation". If having something be implemented as a separate activity or fragment will help your app materially — ease of initial development, ease of maintenance, etc. — use it! Just because it is practical to have few activities does not mean it is bad to have more than one activity. We just now have even easier means of not forcing multiple activities where we might not need them.

这篇关于一个完整的非平凡的 Jetpack Compose 支持的应用程序,只有很少(一个?)活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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