java - 为什么我们使用 Lifecycle 感知组件以及 Android 中 Lifecycle Observer 的用途是什么?

标签 java android

我们的 Activity/Fragment 中已经有一个生命周期,那么为什么我们要使用生命周期感知组件并请指导我它的主要目的。 如果我们使用生命周期感知,那么为什么我们使用我们已经知道的生命周期

最佳答案

是的,Android Activity 有一个所谓的 lifecycle

但这与 Lifecycle aware component 无关

Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain.

A common pattern is to implement the actions of the dependent components in the lifecycle methods of activities and fragments. However, this pattern leads to a poor organization of the code and to the proliferation of errors. By using lifecycle-aware components, you can move the code of dependent components out of the lifecycle methods and into the components themselves.

这是一个tutorial动手操作,让其更容易理解

那么,为什么 Google Android 开发人员在 Android jetpack 中提出了这项技术和其他技术? ?是的,这里讨论的生命周期感知组件来自Android jetpack from 2018. 。他们这样做是为了让我们更轻松地开发强大的 Android 应用程序。他们这样做是为了让我们更接近 SOLID原则。在此之前,开发人员使用自制的 MVC、MVVC 或 MVP 提出自己的风格。多年来关于单例技术的大讨论,当然还有我们如何最好地做到 separation of concern and Single responsibility principle

现在所有这些都已为我们解决了 Android jetpack

In object-oriented computer programming, SOLID is a mnemonic acronym for five design principles intended to make software designs more understandable, flexible and maintainable

WIKI

关于java - 为什么我们使用 Lifecycle 感知组件以及 Android 中 Lifecycle Observer 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56363554/

相关文章:

java - 支柱 : How to iterate until a value is reached

java - 使用 FTPClient 上传到 FTP 的图像为 0k

java - getSelectedItem().toString() 返回 null 值

java - 使用 android fragment 动画共享元素转换似乎是一场噩梦

java - 使线程跳过从其他线程读取输入操作

Android openOptionsMenu 在 KitKat 中什么都不做

android - 如果我通过捏合、拖动和缩放移动和调整图像大小,我怎么知道我触摸了图像的哪个位置

android - 我想在通过 firebase 推送通知发送的通知中发送特定链接,该怎么做?

java - 如何将默认的 Eclipse Oxygen 编译器合规级别从 9 更改为 1.8,而不是将其恢复为 9?

java - spring-boot 应用程序中的缓慢单元测试