gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:50:18 25 4
gpt4 key购买 nike

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

最佳答案

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

但这与 Lifecycle aware component 无关

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

A common pattern is to implement the actions of the dependentcomponents in the lifecycle methods of activities and fragments.However, this pattern leads to a poor organization of the code and tothe proliferation of errors. By using lifecycle-aware components, youcan move the code of dependent components out of the lifecycle methodsand 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 acronymfor five design principles intended to make software designs moreunderstandable, flexible and maintainable

WIKI

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

25 4 0
文章推荐: java - 如何查找按日期排序的 List