上下文失效后使用通过android Context获取的对象
我已经使用 android 有一段时间了,对这个平台感觉很舒服,但我对上下文对象的生命周期有点困惑。查看层次结构,很容易看出 Activity 和 Service 都扩展了 Context,虽然这很方便,但也令人担忧。我避免让需要共享资源的辅助类有一个保存上下文的静态字段(因为几乎所有资源都通过与 Context 对象的交互来实现),这样当活动被销毁时,GC 就可以在以下位置自由释放它:任何时候,但我想知道从上下文中获取的资源。
例如,如果我有一个静态字段,在类内部保存一个文件。然后让这个类的构造函数获取当前上下文,并为 File 分配一个通过传入的 Context 获取的文件资源,在我的第二个类中对 Context 不执行任何其他操作,我是否仍然以某种方式保留着 Context?
class testClass{
private static File someFile;
public testClass(Context context){
synchronized(testClass.class){
if(someFile!=null){
//even though I am holding a File, or a SharedPreference Object generated from this context, am I correctly preventing this utility class from holding the Activity object in memory for no reason?
someFile = context.openFileOutput("Some_File.txt", Context.MODE_PRIVATE);
}
}
}
}
我刚刚读到了有关 Context.getApplicationContext() 的内容(遗憾的是不是静态的)。它说它返回相对于流程而不是活动的上下文,因此如果我需要保留上下文,请使用该上下文。但上面的问题仍然存在。
I have been working with android for a little while now and feel pretty comfortable with the platform, but I have gotten a little confused with the Lifecycle of Context Objects. Looking at the hierarchy it is easy to see that Activity and Service both extend Context, and while this is convenient, it is concerning. I have avoided making helper classes that need a shared resource have a static field holding a context (since just about all resources come through some interaction with a Context object) so that way when an activity is destroyed, the GC is free to free it at any time, but I am wondering about resources fetched from a Context.
For example, if I have a static field that holds a File inside of a class. Then make this class's constructor take the current context and assign the File a File resource fetched through the Context passed in, the do nothing else with the Context in my 2ndary class, am I still holding on in some way to the Context?
class testClass{
private static File someFile;
public testClass(Context context){
synchronized(testClass.class){
if(someFile!=null){
//even though I am holding a File, or a SharedPreference Object generated from this context, am I correctly preventing this utility class from holding the Activity object in memory for no reason?
someFile = context.openFileOutput("Some_File.txt", Context.MODE_PRIVATE);
}
}
}
}
I did just read about Context.getApplicationContext() (Sadly not static). It says it returns a context relative to the process and not the activity so if I need to keep a context around, use that one. But the question above still remains.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我记得我问过这个问题并想我会回答它。
尽管上下文可能有多种,但开发人员使用的主要上下文是活动上下文和应用程序上下文(以及服务上下文等其他内容)。 Activity 上下文是随 Activity 一起创建和销毁的,因此将其用作 Activity 创建和销毁之间存储的常量引用并不是一个好主意。应用程序上下文不具备活动上下文所具有的某些功能,但您需要静态上下文引用的所有内容都在那里(文件 IO、首选项...)。应用程序上下文也随应用程序一起创建和销毁,因此您可以保证只要您的应用程序代码正在运行,上下文就有效。
因此,应用程序上下文应该用于诸如工作线程之类的事情,这些线程可能需要对上下文的恒定访问点,但不需要访问活动。我学到的最好方法是扩展 android Application 类。该类是在内存中创建应用程序时创建的,一旦调用 Application onCreate 方法,Application Context 就有效。这意味着您可以在自定义应用程序类中创建一个静态函数来提供对上下文的访问。
要完成这项工作,您唯一需要做的就是修改您的清单文件,以便 Android 知道使用您的应用程序类而不是默认的应用程序类。
I remembered I asked this question and thought I would answer it.
Though there may be more kinds of contexts, the primary ones developers use are the Activity Context, and the Application Context (and other things like Service Context). The Activity context is created and destroyed with the activity, so it is not a good idea to use as a constant reference stored between activity creation and destruction. The Application Context doesn't have some of the things an Activity Context has, but everything you would want a static context reference for is there (file IO, preferences...). The application context is also created and destroyed with the application, so you can guarantee that as long as your application code is running, the context is valid.
Because of this, the Application context should be used for things like worker threads that may need a constant access point to a context but not need access to an activity. The best way I have learned to do this is to extend the android Application class. This class is created when the application is created in memory, and as soon as the Application onCreate method is called, the Application Context is valid. This means you can create a static function in your custom application class that gives access to the context.
The only other thing you need to make this work is a modification to your manifest file so android knows to use your application class instead of the default.