Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Presenter base class
public abstract class BasePresenter<V extends RemoteView> {
private WeakReference<V> view = null;
public final void attachView(V view) {
if (view == null) throw new NullPointerException("View must not be null");
if(this.view != null) detachView(this.view.get());
this.view = new WeakReference<V>(view);
public final void detachView(V view) {
if (view == null) throw new NullPointerException("Detached view must not be null");
this.view = null;
protected final boolean isViewAttached() {
return view != null;
protected final V getView() {
if (view == null) throw new NullPointerException("getView called when view is null. " +
"Make sure to setView(View) is called first!");
return view.get();

iRYO400 commented Jan 4, 2018

Why do you choose WeakReference<V> instead of just V?

iRYO400 commented Jan 4, 2018

And I see that WeakReference has clear method, mby it's better than assigning to null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment