简体   繁体   English

Android-全局变量?

[英]Android - Global variables?

I need to stock some datas in my application. 我需要在应用程序中存储一些数据。 I know that i can do it like this: 我知道我可以这样做:

class: 类:

public class MyApplication extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

Implementation: 实现方式:

MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getSomeVariable();

This is working if i'm in an activity. 如果我正在活动中,这是正常的。

But if i'm in a class not extended from Activity, how can I access at my datas? 但是,如果我所在的班级没有从Activity扩展过来,该如何访问我的数据?

thanks in advance for your help! 在此先感谢您的帮助!

You can use a Singleton design pattern. 您可以使用Singleton设计模式。 You can then use it anywhere, because it has static access. 然后,您可以在任何地方使用它,因为它具有静态访问权限。

public class SingletonClass {
private static SingletonClass _instance = null;
private int _value = 0;

private SingletonClass() {
}

public static SingletonClass getInstance() {
    if (_instance == null)
        _instance = new SingletonClass();
    return _instance;
}

public int getValue() {
    return _value;
}

public void setValue(int value) {
    _value = value;
}

} }

and then access it like this: 然后像这样访问它:

SingletonClass.getInstance().getValue();

Note: This is a good and easy workaround for some programming problems, but use it very wisely.. it comes with it's problems 注意:对于某些编程问题,这是一种很好且容易的解决方法,但是请非常明智地使用它。

Perhaps by injecting all the required for a class data via constructor or special setter, I would suggest former one. 也许通过构造函数或特殊的设置器注入类数据的所有必需条件,我建议使用前一种。 ( Constructor Injection vs. Setter Injection ) 构造函数注入vs. Setter注入

There are more solutions like static fields but personally I do not like this approach since statics sometimes makes unit testing a bit messy. 还有更多解决方案,例如静态字段,但我个人不喜欢这种方法,因为静态有时会使单元测试有些混乱。

BTW, what kind of variables you want to share? 顺便说一句,您想共享哪种变量?

I use, it may be gruesome to some, a class with static variables, that you can retrieve from every class in the app. 我用的是一个带有静态变量的类,可能有些讨厌,您可以从应用程序的每个类中检索它们。

Just create a class with all the field as static, and you can use them throughout your app. 只需创建一个所有字段均为静态的类,即可在整个应用程序中使用它们。 It doesn't get erased, only when stopping the app. 仅在停止应用程序时,它不会被擦除。

You could also just add static variables to your application class. 您也可以只将静态变量添加到您的应用程序类。

You can use static methods (or variables if they are public). 您可以使用静态方法(或变量,如果它们是公共的)。 It's really a little messy, but if you group them (methods) in the right way you'll earn happinnes and satisfaction ) 确实有点混乱,但是如果您以正确的方式对它们(方法)进行分组,您将获得幸福感和满意度)

static public int getSomeInt(){
     //something
}

And then anywhere in your app use 然后在您的应用程序中的任何地方使用

int x=MyApplication.getSomeInt();

By the way, using this style, you don't need to extend Application class. 顺便说一句,使用这种样式,您无需扩展Application类。 It's better to create an abstract class for such purposes. 为此,最好创建一个抽象类。

Pass the context of your activity as a param to the method or class: 将活动的上下文作为参数传递给方法或类:

// ...
public void doStuff(Context context) {
    // for example, to retrieve and EditText
    EditText et = context.findViewById(R.id.editText1);
}

then, on your activity, you would do: 然后,根据您的活动,您将执行以下操作:

// ...
MyClass myClass = new MyClass();
// ...
myClass.doStuff(this);
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM