
io.taig.android.mosby.MvpFragment Maven / Gradle / Ivy
/*
* 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 io.taig.android.mosby;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.hannesdorfmann.mosby3.mvp.delegate.FragmentMvpDelegate;
import com.hannesdorfmann.mosby3.mvp.delegate.MvpDelegateCallback;
import io.taig.android.mosby.delegate.FragmentMvpDelegateImpl;
/**
* A Fragment that uses a {@link MvpPresenter} to implement a Model-View-Presenter architecture.
*
* @author Hannes Dorfmann
* @since 1.0.0
*/
public abstract class MvpFragment> extends Fragment
implements MvpDelegateCallback, MvpView {
protected FragmentMvpDelegate mvpDelegate;
/**
* The presenter for this view. Will be instantiated with {@link #createPresenter()}
*/
protected P presenter;
/**
* Creates a new presenter instance, if needed. Will reuse the previous presenter instance if
* {@link #setRetainInstance(boolean)} is set to true. This method will be called from
* {@link #onViewCreated(View, Bundle)}
*/
public abstract P createPresenter();
/**
* Gets the mvp delegate. This is internally used for creating presenter, attaching and
* detaching view from presenter.
*
*
* Please note that only one instance of mvp delegate should be used per fragment instance.
*
*
*
* Only override this method if you really know what you are doing.
*
*
* @return {@link FragmentMvpDelegateImpl}
*/
@NonNull
protected FragmentMvpDelegate getMvpDelegate() {
if (mvpDelegate == null) {
mvpDelegate = new FragmentMvpDelegateImpl<>(this, this, true, true);
}
return mvpDelegate;
}
@NonNull
@Override
public P getPresenter() {
return presenter;
}
@Override
public void setPresenter( @NonNull P presenter) {
this.presenter = presenter;
}
@NonNull
@Override
public V getMvpView() {
return (V) this;
}
@Override
public void onViewCreated( View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getMvpDelegate().onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroyView() {
super.onDestroyView();
getMvpDelegate().onDestroyView();
}
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMvpDelegate().onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
getMvpDelegate().onDestroy();
}
@Override
public void onPause() {
super.onPause();
getMvpDelegate().onPause();
}
@Override
public void onResume() {
super.onResume();
getMvpDelegate().onResume();
}
@Override
public void onStart() {
super.onStart();
getMvpDelegate().onStart();
}
@Override
public void onStop() {
super.onStop();
getMvpDelegate().onStop();
}
@Override
public void onActivityCreated( @Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getMvpDelegate().onActivityCreated(savedInstanceState);
}
@SuppressWarnings( "deprecation" )
@Override
public void onAttach( Activity activity) {
super.onAttach(activity);
getMvpDelegate().onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
getMvpDelegate().onDetach();
}
@Override
public void onSaveInstanceState( Bundle outState) {
super.onSaveInstanceState(outState);
getMvpDelegate().onSaveInstanceState(outState);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy