Created
November 26, 2016 10:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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: