Skip to content

Commit

Permalink
change default presenter retain behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
enuoCM committed Apr 9, 2018
1 parent 2b3d35e commit 3e20da8
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/main/java/com/xixicm/ca/presentation/mvp/MvpActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,44 @@ protected void onInitializePresenter(Bundle savedInstanceState) {

/**
* Whether retain {@link #mPresenter} by NonConfigurationInstance.
* If return true, should override {@link #getPresenterFromNonConfigurationInstance()} to get {@link #mPresenter}.
* If return true, should override {@link #getPresenterFromNonConfigurationInstance()} to get {@link #mPresenter} if need.
* If return false, {@link #mPresenter} will be destroyed when this activity {@link #onDestroy}
*
* @return default false
* @return default true
*/
protected boolean isRetainPresenterByNonConfigurationInstance() {
return false;
return true;
}

/**
* If {@link #isRetainPresenterByNonConfigurationInstance} return true, should override this function to get {@link #mPresenter}.
* If {@link #isRetainPresenterByNonConfigurationInstance} return true and {@link #getLastCustomNonConfigurationInstance} doesn't return mPresenter directly,
* should override this function to get {@link #mPresenter}.
*
* @return default null
* @return {@link #mPresenter} or null
*/
@Nullable
protected P getPresenterFromNonConfigurationInstance() {
if (mPresenter != null) {
// this only happens when getPresenter() is called before MvpActivity#onCreatePresenter
return mPresenter;
}
Object o = getLastCustomNonConfigurationInstance();
if (o instanceof MvpPresenter) {
//noinspection unchecked
return (P) o;
}
return null;
}

/**
* Default to return the {@link #mPresenter}.
*
* @return
*/
@Override
public Object onRetainCustomNonConfigurationInstance() {
return mPresenter;
}

@Override
public void onSaveInstanceState(Bundle outState) {
Expand Down Expand Up @@ -128,6 +147,7 @@ protected void onDestroy() {
/**
* Get {@link #mPresenter}. If mPresenter is null, try to get it from {@link #getPresenterFromNonConfigurationInstance} first,
* if it's till null, create it by {@link #createPresenter}, otherwise return it directly
*
* @return
*/
@NonNull
Expand Down

0 comments on commit 3e20da8

Please sign in to comment.