我在非 Activity 中使用 Context 对象,它工作正常,但问题是它显示警告
That is where I am using the context object
Here is the result of inspection
您可以使用弱引用 https://developer.android.com/reference/java/lang/ref/WeakReference.html对于这个案例。像这样的东西:
public class ContactsTask {
private WeakReference<Context> weakContext;
public ContactsTask(Context context){
weakContext = new WeakReference<>(context);
}
public void doSomething(){
if (weakContext!=null) weakContext.get() ... //return context without leaks
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)