Android - 在 onStart() 中设置接口并在 onStop() 中清除它们

Android - Setting interfaces in onStart() and clearing them in onStop()

我在onStart()里面看到很多Android代码设置接口的例子;方法并在 onStop();

中用 null 清除它们

例如:

@Overrride
public void onStart(){
    button.setOnClickListener(this);
}

@Override
public void onStop(){
    button.setOnClickListener(null);
}

这是为什么?这是好的编码习惯吗?

仅当没有活动(非守护进程)线程引用 Object.While 时,对象才有资格进行垃圾回收,android.app.Activity 的生命周期理论上会在调用 onDestroy() 后完成方法,它仍然需要从任何可能持有对 it.Therefore 的引用的对象中取消引用,上面的代码试图从 Button 对象中删除它自己的引用,以便它可能有资格进行垃圾收集。