Skip to content

Instantly share code, notes, and snippets.

@aemxn
Created August 24, 2017 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aemxn/cfa36bdf11d4b54704a8b3a05cd4a01d to your computer and use it in GitHub Desktop.
Save aemxn/cfa36bdf11d4b54704a8b3a05cd4a01d to your computer and use it in GitHub Desktop.
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
Copy link

iRYO400 commented Jan 4, 2018

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

@iRYO400
Copy link

iRYO400 commented Jan 4, 2018

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

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