Skip to content

Instantly share code, notes, and snippets.

@xanderblinov
Created November 26, 2016 10:15
Show Gist options
  • Save xanderblinov/c2fa5c2250750c7e0b5356de56596153 to your computer and use it in GitHub Desktop.
Save xanderblinov/c2fa5c2250750c7e0b5356de56596153 to your computer and use it in GitHub Desktop.
How to prevent Moxy's delegate onDestroy call of fragment in backstack while configuration changing.
public void onDestroy() {
super.onDestroy();
boolean anyParentIsRemoving = false;
for (Fragment parent = this.getParentFragment(); !anyParentIsRemoving && parent != null;
parent = parent.getParentFragment()) {
anyParentIsRemoving = parent.isRemoving();
}
boolean callOnDestroy = false;
if (this.isRemoving() || anyParentIsRemoving || this.getActivity().isFinishing()) {
callOnDestroy = true;
}
// prevent removing fragments from back stack
for (int i = 0; i < getFragmentManager().getBackStackEntryCount(); i++) {
if (TextUtils.equals(getFragmentManager().getBackStackEntryAt(i).getName(), getTag())) {
callOnDestroy = false;
break;
}
}
if(callOnDestroy){
this.getMvpDelegate().onDestroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment