У меня есть два класса - первый: < /p>
Код: Выделить всё
public class Utility {
private static Utility instance = null;
private UpdateListener listener;
// Make it a Singleton class
private Utility() {}
public static Utility getInstance() {
if (instance == null)
instance = new Utility();
return instance;
}
public void setListener(UpdateListener listener) {
this.listener = listener;
}
// Long running background thread
public void startNewTread() {
new Thread (new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000 * 10);
if (listener != null)
listener.onUpdate();
} catch (InterruptedException e) {
Log.d("Utility", e.getMessage());
}
}
}).start();
}
// Listener interface
public interface UpdateListener {
public void onUpdate();
}
}
< /code>
Второй класс: < /p>
public class ListenerLeak extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Setting the listener
Utility.getInstance().setListener(new Utility.UpdateListener() {
@Override
public void onUpdate() {
Log.d("ListenerLeak", "Something is updated!");
}
});
//Starting a background thread
Utility.getInstance().startNewTread();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
< /code>
В этой деятельности. May New Utility.updateListener Подробнее здесь: https://stackoverflow.com/questions/530 ... -referance