0

我试图在AsyncTask. 为了处理可能的设备轮换,我将AsyncTask实例保存onRetainNonConfigurationInstance()onCreate(). 到目前为止,一切都很好...

但是,由于 Activity 是在设备旋转后重新创建的,因此我必须将Activity我的所有引用更新AsyncTask为新创建的引用。问题是,SQLiteOpenHelper内部AsyncTask还有一个上下文实例,它通过其构造函数传递,而我Activity之前只是传递了旧实例,现在如何将其更新为新实例Activity

我已经测试过了,即使我不更新SQLiteOpenHelper. 但我仍然不确定它是否可以。

谢谢

4

3 回答 3

1

You should rather use the application context (getApplicationContext()) instead of the activity one (this). If you use the activity context you leak that

于 2012-09-10T19:18:20.387 回答
1

You are leaking an Activity instance when you do this, since the garbage collector cannot free it up. Depending on your app, this may or may not be fatal to your memory usage, but it's not good in any case.

You should either use the application Context for your database or create a ContentProvider which has its own context to use.

于 2012-09-10T19:19:33.007 回答
0

我发现了一篇关于上下文和内存泄漏的令人印象深刻的文章: http ://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

于 2012-09-11T08:11:06.140 回答