最佳实践分享活动之间的全局变量全局变量

2023-09-07 01:12:47 作者:薰衣草の眼泪

我在如何共享活动之间的全局变量在被认为是安全的Andr​​oid项目有点混乱。

什么是做到这一点的最佳做法?扩展应用类或创建一个自定义的单个类?

这是帮助AP precciate,先谢谢了。

解决方案

在应用程序类存储东西的问题是,你不能在一个活动是从应用对象的同一实例恢复计数。例如,一个活动可以被暂停时,应用程序杀死(由于存储器)以及任何变化所做的实例对象,然后活性用新的应用程序实例续

下面是一个很好的博客文章表示存储在应用程序类的数据如何丢失: http://www.developerphil.com/dont-store-data -in-的应用对象

我不知道这是非常最好的做法,但我觉得这是一个很好的解决方案。

我有一个PersistData类包含应用程序范围全局。我用匕首注入这一单实例到需要这些变量的任何类。

基本过程是这样的:

Python线程 多线程共享全局变量

当我通过像保存在这个对象的值:

  mPersistData.saveEmailId(me@example.com); 

我第一次写在共享preferences 条目我然后将其保存到一个成员变量

在构造函数类的,我从共享preferences阅读它们初始化的成员变量。这种方式读取的变量是缓存,即他们无需从共享preferences阅读做的,如果应用程序杀过并重启成员变量有正确的值。如果你只是保持在应用程序类中的值,重新启动应用程序时,成员变量将是无效的(你期望不算什么,甚至为空)。

下面是一个例子:

 公共类PersistData {    私人字符串电子邮件;    公共PersistData(MyApp的应用){        MAPP =应用;        电子邮件= readEmailId();    }    公共无效saveEmailId(字符串emailToSave){        writeEmailId(emailToSave);        电子邮件= emailToSave;    }    私人无效writeEmailId(字符串EMAILID){        generalSettingsFileEditor.putString(USER_ID,EMAILID);        generalSettingsFileEditor.commit();    }    公共字符串readEmailId(){        串emaiId = generalSettingsFile.getString(USER_ID,);        返回(emaiId);    }    公共字符串getEmail(){        返回的电子邮件;    }} 

在我的应用程序匕首模块我有这样的:

  @Provides @Singleton公共PersistData providePersistData(){    的System.out.println(的String.format(MAPP =%S,MAPP));    返回新PersistData(MAPP);} 

然后每当我需要访问这些变量我注入单身如此的:

 公共类家庭$ P $ {psenter    @注入    PersistData mPersistData;    ...    mPersistData.saveEmailId(me@example.com);    myEmail = mPersistData.getEmailId();   ...} 

I'm a bit confusing on how to share global variables between activities in an android project that considered safe.

What is the best practice to do that? Extends Application class or make a custom singleton class?

An help is apprecciate, thanks in advance.

解决方案

The problem with storing something in the application class is you cannot count on an activity being resumed from the same instance of the application object. For example an activity can be paused, the application killed (due to memory) along with any changes you made to object in the instance, and then the activity resumed with a new application instance.

Here is a very good BLOG post explaining how data stored in the application class can be lost: http://www.developerphil.com/dont-store-data-in-the-application-object

I am not sure this is the very "Best" practice, but I think this is a good solution

I have a PersistData class holds application wide "globals". I use Dagger to inject instances of this singleton into any class that requires these variables.

The basic process it this:

When I save a value in this object via something like:

mPersistData.saveEmailId("me@example.com");

I first write it to an entry in SharedPreferences I then save it to a member variable

In the constructor for the class, I initialize the member variables by reading them from SharedPreferences. This way reads for the variable are "cached", ie they don't need to be read from SharedPreferences, and if the application is ever killed and restarted the member variables have the correct values. If you just hold the values in the application class, when the application is restarted the member variables will be invalid (not what you expect or even null).

Here is an example:

public class PersistData {
    private String email; 
    public PersistData(MyApp app) {
        mApp = app;
        email = readEmailId();

    }

    public void saveEmailId(String emailToSave) {
        writeEmailId(emailToSave);
        email = emailToSave;
    }

    private void writeEmailId(String emailId) {
        generalSettingsFileEditor.putString("USER_ID", emailId);
        generalSettingsFileEditor.commit();
    }

    public String readEmailId() {
        String emaiId = generalSettingsFile.getString("USER_ID","");
        return(emaiId);
    }

    public String getEmail() {
        return email;
    }
}

In my application Dagger module I have this:

@Provides @Singleton
public PersistData providePersistData () {
    System.out.println(String.format("mApp = %s", mApp));
    return new PersistData(mApp);
}

Then whenever I need to access any of these variables I inject the singleton as so:

public class HomePresenter {
    @Inject
    PersistData mPersistData;
    ...

    mPersistData.saveEmailId("me@example.com");
    myEmail = mPersistData.getEmailId();

   ...
}