我正在开发一个使用一些后台任务 (AsyncTasks) 的 Android 应用程序,我想使用有关跨应用程序生命周期和任务回调的数据持久性的最佳实践。
到目前为止,我对此有多种做法:
1) 我在以以下形式使用 AsyncTasks 的类中有一些静态字段:
private static String str1;
private static String str2;
private static int int1;
...//=>no more than 6 static fields
2) 我使用带有许多 getter/setter 的 sinleton App 实例,形式如下:
package xxx.xxx.xxx
import xxx.xxx.xxx
...
public class AppSettings {
private static AppSettings singleton;
private String _field1;
...//=>many fields
public void setField1(String field1) { _field1 = field1; }
public String getField1() { return _field1; }
...//=>many getters/setters
private AppSettings() {}
public AppSettings getInstance(){
if (instance== null) {
synchronized(AppSettings.class) {
if (instance == null)
instance = new AppSettings();
}
}
return instance;
}
}
我绝对知道滥用静态字段一点也不好,所以我决定全部替换它们,但我不完全确定我的第二种方法 - 在具有许多 getters/setters 的单例中有一个应用程序实例 -被认为是一个很好的方法,如果不是,我想知道更好的选择。
非常感谢。
编辑 1:只是为了澄清。
为了让您更清楚地了解我使用 AppSettings 单例类的目的,我将举两个示例:
1) 我用它来存储应用程序设置/配置值(这就是名称的原因)以便在任何地方都可用。例如,字体颜色、字体大小等等。
2) 我用它来存储临时数据/值。例如,我的主要 Activity 使用“VideoHelper”类在背景中创建了一个小视频并通过 AsyncTask 调用,并且由于视频生成过程需要来自主要 Activity 的一些参数,我使用 AppSettings getters/setters 来发送它们。
编辑 2:更好地解释一切。
感谢@a_local_nobody,我意识到我的“使用案例”不是很清楚,所以我将添加更多内容。
我的 AppSettings 未用于存储用户设置,为此我使用 SharedPreferences,而是应用程序默认配置参数。
举个例子,我存储 Activity 背景颜色(这只是一个例子)所以如果将来我改变主意并决定使用另一种背景颜色,这个设置(以及更多)集中在那里。它就像许多默认应用设置的“容器”。
关于在这个应用程序单例类中使用 getter 和 setter,我想我会遵循@a_local_nobody 的建议,在每个类中定义一些静态变量并根据需要使用它们,而不是在全局范围内使用一堆不相关的 getter/setter .
无论如何,欢迎大家提出意见。
最佳答案
好吧,你在谈论跨应用程序生命周期持久化数据
,在我看来,这听起来像是你在寻找一个ViewModel
:
The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.
还有:
The purpose of the ViewModel is to acquire and keep the information that is necessary for an Activity or a Fragment. The Activity or the Fragment should be able to observe changes in the ViewModel.
ViewModels 构成了 MVVM 设计模式的一部分,在线提供了大量示例。
更多信息,看看at the documentation
在旁注中,也许你可以看看 google sunflower project有关如何实现新架构组件的一些想法,其中包括 ViewModels
的用法。
另外值得补充的是,您使用 AppSettings
解决方案创建的内容是一个很大的依赖项。很多事情都取决于这个单一的对象,并且很可能在整个应用程序中都需要它。您可能会考虑,而不是像这样创建它,而是使用 dependency injection根据您的选择,对于 Android,可能是 Dagger 2或 Koin for kotlin (如果你曾经切换到 kotlin)或者你自己的依赖注入(inject)形式,而不必使用这些框架。
希望对你有帮助
根据 OP 的反馈进行编辑:
I use it to store app setting/configuration values (that's why the name) to be available anywhere. For example, font color, font size, whatever.
这听起来像是 Shared preferences 的更好用例,特别是如果这些是由用户定义的设置,否则您应该将它们保存到 strings.xml
等中并使用 localization
I use it to store temporary data/values. For example, my main activity creates a small video in the background using "VideoHelper" class and called through an AsyncTask, and as video generation process needs some parameters from main activity, I use AppSettings getters/setters to send them through.
如果您有一个 VideoHelper
类,您最好为这个对象创建一个 Builder 设计模式,或者为这个助手提供静态变量以根据需要更改其功能,如果这些是您的 VideoHelper
的变量,那么它们应该与您的 VideoHelper
一起定位。
一起变化的事物通常应该保持在一起。
关于Android:跨应用程序生命周期持久化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58764294/