nucleus.view.NucleusActivity Maven / Gradle / Ivy
Show all versions of nucleus Show documentation
package nucleus.view;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import nucleus.factory.PresenterFactory;
import nucleus.factory.ReflectionPresenterFactory;
import nucleus.presenter.Presenter;
/**
* This class is an example of how an activity could controls it's presenter.
* You can inherit from this class or copy/paste this class's code to
* create your own view implementation.
*
* @param a type of presenter to return with {@link #getPresenter}.
*/
public abstract class NucleusActivity
extends Activity implements ViewWithPresenter
{
private static final String PRESENTER_STATE_KEY = "presenter_state";
private PresenterLifecycleDelegate
presenterDelegate =
new PresenterLifecycleDelegate<>(ReflectionPresenterFactory.
fromViewClass(getClass()));
/**
* Returns a current presenter factory.
*/
public PresenterFactory
getPresenterFactory() {
return presenterDelegate.getPresenterFactory();
}
/**
* Sets a presenter factory.
* Call this method before onCreate/onFinishInflate to override default {@link ReflectionPresenterFactory} presenter factory.
* Use this method for presenter dependency injection.
*/
@Override
public void setPresenterFactory(PresenterFactory
presenterFactory) {
presenterDelegate.setPresenterFactory(presenterFactory);
}
/**
* Returns a current attached presenter.
* This method is guaranteed to return a non-null value between
* onResume/onPause and onAttachedToWindow/onDetachedFromWindow calls
* if the presenter factory returns a non-null value.
*
* @return a currently attached presenter or null.
*/
public P getPresenter() {
return presenterDelegate.getPresenter();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
presenterDelegate.onRestoreInstanceState(savedInstanceState.getBundle(PRESENTER_STATE_KEY));
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(PRESENTER_STATE_KEY, presenterDelegate.onSaveInstanceState());
}
@Override
protected void onResume() {
super.onResume();
presenterDelegate.onResume(this);
}
@Override
protected void onPause() {
super.onPause();
presenterDelegate.onDropView();
}
@Override
protected void onDestroy() {
super.onDestroy();
presenterDelegate.onDestroy(!isChangingConfigurations());
}
}