Android:跨应用程序生命周期持久化数据

Android: persisting data across app lifecycle

我正在开发一个使用一些后台任务 (AsyncTasks) 的 Android 应用程序,我想使用有关跨应用程序生命周期和任务回调的数据持久性的最佳实践。

到目前为止,我对此有多种做法:

1) 我在 classes 中有一些静态字段,其中 AsyncTasks 以以下形式使用:

private static String str1;
private static String str2;
private static int int1;
...//=>no more than 6 static fields

2) 我使用一个 sinleton App 实例,其中包含许多 getters/setters,形式为:

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 单例 class 我会给您举两个例子:

1) 我用它来存储应用程序 setting/configuration 值(这就是名称的原因)以便在任何地方都可用。比如字体颜色,字体大小等等。

2) 我用它来临时存储data/values。例如,我的 main activity 使用 "VideoHelper" class 在后台创建了一个小视频并通过 AsyncTask 调用,并且由于视频生成过程需要来自 main activity 的一些参数,我使用 AppSettings getters/setters 来发送它们。

编辑 2:更好地解释所有内容。

感谢@a_local_nobody 我意识到我的 "case of use" 不是很清楚所以我再补充一些东西。

我的 AppSettings 未用于存储用户设置,我为此使用 SharedPreferences,而是应用程序默认配置参数。

举个例子,我存储活动背景颜色(这只是一个例子)所以如果将来我改变主意并决定使用另一种背景颜色,这个设置(以及更多)集中在那里。对于许多默认应用设置,它就像 "container"。

关于在这个应用程序单例中使用 getter 和 setter class,我想我会遵循@a_local_nobody 的建议,在每个 class 中定义一些静态变量并使用他们根据需要而不是在全球范围内拥有一堆不相关的getters/setters。

无论如何,欢迎大家提出意见。

您的方法在现代 android 开发中不符合 "best practices" 的要求。

处理配置更改的推荐方法是使用新的 architecture component: ViewModel

它具有属性在发生配置更改时触发的 onDestroy 幸存。

基本上,您需要将此 AppSettings 代码移至 ViewModel。

好吧,你说的是 persisting data across app lifecycle,在我看来,这听起来像是你在寻找 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 with your options, for android, probably being either Dagger 2 or Koin for kotlin(如果您曾经切换到 kotlin)或者您自己的依赖注入形式,而不必使用这些框架。

希望对您有所帮助


根据 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, especially if these are settings defined by a user, otherwise you should be savings these into strings.xml etc. and making use of 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 class,您最好为这个对象创建一个 Builder 设计模式,或者为这个助手提供静态变量以根据需要更改其功能,如果这些是您的 VideoHelper 的变量,那么它们应该与您的 VideoHelper 一起定位。

一起变化的事物通常应该保持在一起。