Skip to content

Instantly share code, notes, and snippets.

@jofor
Created April 1, 2015 07:13
Show Gist options
  • Save jofor/b73e2d9215f04d4ca784 to your computer and use it in GitHub Desktop.
Save jofor/b73e2d9215f04d4ca784 to your computer and use it in GitHub Desktop.
public abstract class NucleusActionBarActivity<PresenterType extends Presenter> extends ActionBarActivity {
private static final String PRESENTER_STATE_KEY = "presenter_state";
private PresenterType presenter;
public NucleusActionBarActivity() {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestPresenter(savedInstanceState == null ? null : savedInstanceState.getBundle("presenter_state"));
}
protected void onDestroy() {
if (this.isFinishing()) {
this.destroyPresenter();
}
super.onDestroy();
}
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle("presenter_state", this.savePresenter());
}
protected void onResume() {
super.onResume();
this.takeView();
}
protected void onPause() {
super.onPause();
this.dropView(this);
}
public PresenterType getPresenter() {
return this.presenter;
}
public void destroyPresenter() {
if (this.presenter != null) {
PresenterManager.getInstance().destroy(this.presenter);
this.presenter = null;
}
}
private void requestPresenter(Bundle presenterState) {
if (this.presenter == null) {
this.presenter = PresenterManager.getInstance().provide(this, presenterState);
}
}
private Bundle savePresenter() {
return PresenterManager.getInstance().save(this.presenter);
}
private void takeView() {
this.requestPresenter((Bundle) null);
this.presenter.takeView(this);
}
private void dropView(Activity activity) {
this.presenter.dropView();
if (activity.isFinishing()) {
this.destroyPresenter();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment