非UI片段VS辛格尔顿格尔、片段、UI、VS

由网友(南仄)分享简介:我想这非UI片段的主要目的是保留在配置更改数据的存储,对不对?所以,从这个被特定的存储为拥有该片段的活动,这是它的使用范围遍及整个应用程序的单例模式(这是我至今为止所做的溶液)的好处APPART?I guess that the main purpose of the non-UI fragments is the...

我想这非UI片段的主要目的是保留在配置更改数据的存储,对不对?所以,从这个被特定的存储为拥有该片段的活动,这是它的使用范围遍及整个应用程序的单例模式(这是我至今为止所做的溶液)的好处APPART?

I guess that the main purpose of the non-UI fragments is the storage of data that is retained over configuration changes, right? So, appart from being this storage specific for the Activity that owns this fragment, which is the benefit of its usage over a Singleton pattern across the entire application (which is the solution I've been doing so far)?

推荐答案

这片段的作用范围是其活动的事实意味着有更少的长期内存泄漏的机会,而不是单身 - 片段最终应让垃圾回收,而单不会。

The fact that a fragment is scoped to its activity means there is less chance of a long-term memory leak, as opposed to singletons -- the fragment should eventually get garbage-collected, while the singleton will not.

您也有超过时间稍微控制。在应用只是在你的应用程序的任何 ContentProviders 之后创建的,并且你对此别无选择。相反,您控制片段生成,因此可能能够采取控制的优势。

You also have somewhat more control over timing. The Application is created just after any ContentProviders in your app, and you have no choice on that. Conversely, you control when fragments get created, and therefore may be able to take advantage of that control.

所以,对于确需只能由活动所需要的数据的地方,一个非UI片段可能不是一个单一个更好的主意。该单将是哪里跨多个组件需要数据的地方。

So, for places where the data is really only needed by an activity, a non-UI fragment is probably a better idea than a singleton. The singleton would be for places where the data is needed across multiple components.

阅读全文

相关推荐

最新文章