我在开发者博客上读过有关上下文内存泄漏的内容。
http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html
但我不确定我是否理解它。
由于上下文引用,方法getAppVersion
是否会导致内存泄漏?
public class A
{
public static int getAppVersion(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
// should never happen
throw new RuntimeException("Could not get package name: " + e);
}
}
}
public class B extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
int version = A.getAppVersion(this);
}
}