Created
March 23, 2017 12:28
-
-
Save serhiitereshchenko/55e39484dc81a08c2fd8460ad46aec81 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 class BackStackFragment extends Fragment { | |
private List<Runnable> fragmentOpsStack = new LinkedList<>(); | |
private boolean mSavedInstanceState = true; | |
@Override | |
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { | |
Animation animation = super.onCreateAnimation(transit, enter, nextAnim); | |
if (animation == null && nextAnim != 0) { | |
animation = AnimationUtils.loadAnimation(getActivity(), nextAnim); | |
} | |
if (animation != null) { | |
getView().setLayerType(View.LAYER_TYPE_HARDWARE, null); | |
animation.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
} | |
public void onAnimationEnd(Animation animation) { | |
getView().setLayerType(View.LAYER_TYPE_NONE, null); | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
} | |
return animation; | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mSavedInstanceState = false; | |
Iterator<Runnable> iterator = fragmentOpsStack.iterator(); | |
while (iterator.hasNext()) { | |
Runnable r = iterator.next(); | |
getActivity().runOnUiThread(r); | |
iterator.remove(); | |
} | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
mSavedInstanceState = true; | |
} | |
protected void doInResumedState(Runnable transaction) { | |
if (!mSavedInstanceState) { | |
transaction.run(); | |
} else { | |
fragmentOpsStack.add(transaction); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment