我刚刚阅读了Romain Guy关于如何避免Android内存泄漏的博客文章.
在文章中他给出了这个例子:
private static Drawable sBackground;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
TextView label = new TextView(this);
label.setText("Leaks are bad");
if (sBackground == null) {
sBackground = getDrawable(R.drawable.large_bitmap);
}
label.setBackgroundDrawable(sBackground);
setContentView(label);
}
Run Code Online (Sandbox Code Playgroud)
罗曼说:
这个例子是泄露Context的最简单的例子之一.
我的问题是,你如何正确地修改它?
像这样?
TextView label = new TextView(Context.getApplicationContext());
Run Code Online (Sandbox Code Playgroud)
我测试了两种方式,结果是一样的.我无法找到差异.我认为这this比应用程序上下文更正确.因为this是一个参考Activity,也就是TextView属于那个Activity.
有人可以给我一个解释吗?
android ×1