Skip to content

Instantly share code, notes, and snippets.

@xanderblinov
Created November 26, 2016 10:15
Show Gist options
  • Save xanderblinov/98d623c9e1366b10740731826fc0ecd1 to your computer and use it in GitHub Desktop.
Save xanderblinov/98d623c9e1366b10740731826fc0ecd1 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();
}
}
@xanderblinov
Copy link
Author

Problem:

We have fragments A and B on backstack. (Firstly added A and then replace it with B)
While configuration changed fragment A will removed and it'll got onDestroy() call of delegate, so presenter of A fragment will be destroyed.

Solution:

  • Add every "add" and "replace" transaction the same name with new fragment.
  • Replace onDestroy method of your fragment with code below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment