Created
August 24, 2017 02:27
-
-
Save aemxn/cfa36bdf11d4b54704a8b3a05cd4a01d to your computer and use it in GitHub Desktop.
Presenter base class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
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
Why do you choose
WeakReference<V>
instead of justV
?