All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hannesdorfmann.mosby.mvp.MvpBasePresenter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 Hannes Dorfmann.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hannesdorfmann.mosby.mvp;

import android.support.annotation.Nullable;
import java.lang.ref.WeakReference;

/**
 * A base implementation of a {@link MvpPresenter} that uses a WeakReference for referring
 * to the attached view.
 * 

* You should always check {@link #isViewAttached()} to check if the view is attached to this * presenter before calling {@link #getView()} to access the view. *

* * @param type of the {@link MvpView} * @author Hannes Dorfmann * @see MvpNullObjectBasePresenter * @since 1.0.0 */ public class MvpBasePresenter implements MvpPresenter { private WeakReference viewRef; @Override public void attachView(V view) { viewRef = new WeakReference(view); } /** * Get the attached view. You should always call {@link #isViewAttached()} to check if the view * is * attached to avoid NullPointerExceptions. * * @return null, if view is not attached, otherwise the concrete view instance */ @Nullable public V getView() { return viewRef == null ? null : viewRef.get(); } /** * Checks if a view is attached to this presenter. You should always call this method before * calling {@link #getView()} to get the view instance. */ public boolean isViewAttached() { return viewRef != null && viewRef.get() != null; } @Override public void detachView(boolean retainInstance) { if (viewRef != null) { viewRef.clear(); viewRef = null; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy