将应用程序上下文保存到 Android 中的静态变量是否安全? [英] Is it safe to save the app context to a static variable in Android?

查看:17
本文介绍了将应用程序上下文保存到 Android 中的静态变量是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 Android 上使用静态变量是非常危险的,尤其是当您将它们引用到活动时.但是,如果我有一个扩展 Application 的类(让我们称这个类为App"),那么引用这个类的实例是否安全?

I know that usage of static variables on Android is quite risky, especially if you reference them to activities. However, if I have a class that extends Application (let's call this class "App"), is it safe to reference to the instance of this class?

如果是这样,任何其他类对应用程序上下文有任何类型的引用也是安全的吗?我的意思是,如果我在任何类型的类中引用了应用程序上下文,是否会出现内存泄漏?

If so, is it also safe for any other class to have any kind of reference to the application context? I mean, can there be a memory leak if I have a reference to the application context in any kind of class?

目的是无论我在哪个范围内,我总能得到对应用程序上下文的引用.我认为是安全的,因为如果系统关闭应用程序,静态变量也会消失,直到下次应用程序再次启动时,才会再次初始化静态变量.

The purpose is that no matter in which scope I am in, I can always get a reference to the application context. I think it's safe, since if the system closes the application, the static variable is also gone till the next time the application starts again, which will initialize the static variable again.

另外,这并不重要,但如果我使用多个进程,我会在每个进程上获得对 App 类的完全不同的引用吗?

Also, not that it matters much, but if I use multiple processes, will I get totally different references to App class on each process?

作为代码示例,以下是我的想法:

As an example of code, here's what I'm thinking about:

public class App extends Application
{
    private static Context _appContext;

    @Override
    public void onCreate()
    {
        super.onCreate();
        _appContext = this;
    }

    public static Context getAppContext()
    {
        return _appContext;
    }
}

推荐答案

将应用上下文保存到静态变量是否安全?

is it safe to save the app context to a static variable?

目前,是的,它似乎是安全的,尽管我不会让 getAppContext() 返回 Context,而是返回 AppApplication.

Presently, yes, it appears to be safe, though I would not have getAppContext() return Context, but instead return App or Application.

话虽如此,Android 核心团队一开始并没有这样设置的事实表明,可能存在我们不知道的隐藏问题,或者将来这种方法可能会引入问题.

That being said, the fact that the core Android team did not set it up this way in the first place suggests that perhaps there may be hidden issues of which we are unaware, or that in the future this approach may introduce problems.

俗话说,YMMV.:-)

编辑

如果是这样,任何其他类对应用程序上下文有任何类型的引用也是安全的吗?

if so , is it also safe for any other class to have any kind of reference to the application context ?

我不知道你在这里所说的安全"是什么意思.

I have no idea what you mean by "safe" here.

但是如果我使用多个进程,我将在每个进程上获得对 App 类的完全不同的引用,对吗?

but if i use multiple processes , i will get totally different references to App class on each process , right?

如果你使用多个进程,你应该被鳟鱼打一个耳光.但是,是的,您应该为每个进程获得不同的 App 实例.

If you use multiple processes, you should be slapped with a trout. But, yes, you should get distinct App instances per process.

这篇关于将应用程序上下文保存到 Android 中的静态变量是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆