Skip to content

Instantly share code, notes, and snippets.

Created May 6, 2015 04:10
Show Gist options
  • Save rjrjr/9b54652c98e4a96427d2 to your computer and use it in GitHub Desktop.
Save rjrjr/9b54652c98e4a96427d2 to your computer and use it in GitHub Desktop.
package mortar;
public abstract class NewPresenter<V> {
private V view = null;
private Scoped registration = new Scoped() {
@Override public void onEnterScope(MortarScope scope) {
@Override public void onExitScope() {
* Called to give this presenter control of a view, e.g. from
* {@link android.view.View#onAttachedToWindow()}. (Redundant calls will safely no-op.)
* <p/>
* It is expected that {@link #dropView(Object)} will be called with the same argument when the
* view is no longer active, e.g. from {@link android.view.View#onDetachedFromWindow()}.
public final void takeView(V view) {
if (view == null) throw new NullPointerException("new view must not be null");
if (this.view != view) {
if (this.view != null) {
this.view = view;
* Called to surrender control of this view, e.g. when the view is detached. If and only if
* the given view matches the last passed to {@link #takeView}, the reference to the view is
* cleared.
* <p/>
* Mismatched views are a no-op, not an error. This is to provide protection in the
* not uncommon case that dropView and takeView are called out of order. For example, an
* activity's views are typically inflated in {@link
*}, but are only detached some time after {@link
* onExitScope}. It's possible for a view from one activity
* to be detached well after the window for the next activity has its views inflated&mdash;that
* is, after the next activity's onResume call.
public void dropView(V view) {
if (view == null) {
throw new NullPointerException("dropped view must not be null");
if (view == this.view) {
this.view = null;
/** Called by {@link #takeView}. Given a view instance, return its {@link MortarScope}. */
protected abstract MortarScope extractScope(V view);
protected void onEnterScope(MortarScope scope) {
protected void onTakeView(V view) {
protected void onDropView() {
protected void onExitScope() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment