假设以下两个类:
public class Network {
private static Network instance;
public Network() {
instance = this;
}
public static Network getInstance() {
return instance;
}
public interface Listener {
public void event(String msg);
};
ArrayList<Listener> listeners = new ArrayList<Listener>();
public void addListener(Listener listener) {
listeners.add(listener);
}
}
public class Act1 extends Activity implements Network.Listener {
@Override
public void onCreate(Bundle b) {
Network.getInstance().addListener(this);
}
public void event(String msg) {
// do nothing
}
}
public class Act2 extends Activity implements Network.Listener {
@Override
public void onCreate(Bundle b) {
Network.getInstance().addListener(new Network.Listener() {
public void event(String msg) {
// do nothing
}
);
}
public void event(String msg) {
}
}
一旦用户转到另一个 Activity,这两个 Activity 中的任何一个都会泄漏吗?是否会阻止任何一个 Activity 获得 GCed?上面的 Listener 实现有什么问题吗?