简体   繁体   English

Android:维护全球应用程序状态

[英]Android: Maintaining Global Application State

Android documentation for Application states: There is normally no need to subclass Application. 应用程序状态的Android文档:通常不需要子类Application。 In most situations, static singletons can provide the same functionality [ie maintain global application state] in a more modular way. 在大多数情况下,静态单例可以以更模块化的方式提供相同的功能[即维护全局应用程序状态]。 If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton. 如果你的单例需要一个全局上下文(例如注册广播接收器),那么检索它的函数可以给一个Context,它在第一次构造单例时在内部使用Context.getApplicationContext()。

My request is: Can you explain, and provide code sample that implements the above suggestion for maintaining global state. 我的请求是:您能解释并提供实现上述维护全局状态建议的代码示例。

Note that there is already a suggestion that recommends subclassing Application: How to declare global variables in Android? 请注意,已经建议建议子类化Application: 如何在Android中声明全局变量?

Thank you. 谢谢。

Correction to StinePike's answer regarding context in the ApplicationState. 更正了StinePike关于ApplicationState中上下文的答案。 In the code posted the context passed in to the application state is held on to. 在发布的代码中,传递给应用程序状态的上下文保持在。 If the context passed in is an activity or similar unit then the activity would be leaked and prevented from being garbage collected. 如果传入的上下文是活动或类似单元,则活动将被泄露并防止被垃圾收集。

The android documentation for the Application class states you should "internally use Context.getApplicationContext() when first constructing the singleton." Application类的android 文档声明你应该在第一次构造单例时“在内部使用Context.getApplicationContext() 。”

public class ApplicationState {
    private Context applicationContext;
    private static ApplicationState instance;

    private ApplicationState(Context context) {
        this.applicationContext = context.getApplicationContext();
    }

    public static ApplicationState getInstance(Context context) {
        if(instance == null) {
            instance = new ApplicationState(context);
        }
        return instance;
    }
}

If I am not wrong your are trying to save global variables without extending Application. 如果我没有错,你试图保存全局变量而不扩展Application。 If so you can do two things 如果是这样,你可以做两件事

if you don't need any context then you ca simply use a class with static members like this 如果您不需要任何上下文,那么您只需使用具有此类静态成员的类

public class ApplicationState {
    public static boolean get() {
        return b;
    }

    public static void set(boolean a) {
        b = a;
    }

    private static boolean b;
}

And if you need a context but you don't want to extend Application you can use 如果您需要上下文,但您不想扩展应用程序,则可以使用

Public class ApplicationState {
    private Context context;
    private static ApplicationState instance;

    private ApplicationState(Context context) {
        this.context = context;


    public static ApplicationState getInstance(Context context) {
        if (instance == null) {
            instance = new ApplicationState(context);
        }
        return instance;
    }

    public void someMethod(){}
}

So you can call some method like this ApplicationState.getInstance(context).somemethod(); 所以你可以调用一些像这样的ApplicationState.getInstance(context).somemethod();

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

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