Fragment lost transition animation after configuration change

前端 未结 5 1149
独厮守ぢ
独厮守ぢ 2021-02-01 15:35

I\'m inserting Fragments into the Activity using this code:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setCon         


        
相关标签:
5条回答
  • 2021-02-01 15:57

    As a workaround for this you can use onCreateAnimator/onCreateAnimation methods in your fragments.

    For example for native fragments implementation:

    @Override
    public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
        if (enter) {
            return AnimatorInflater.loadAnimator(getActivity(), R.animator.slide_in_top);
        } else {
            return AnimatorInflater.loadAnimator(getActivity(), R.animator.fade_out);
        }
    }
    

    The same technique for support library fragments with Animation instead. In this case you also have more control over how would you like to play animation depending upon fragment state and/or arguments.

    0 讨论(0)
  • 2021-02-01 16:08

    An alternative suggestion to work around this issue is to download the source for the support library and make the change I suggested in the defect (http://code.google.com/p/android/issues/detail?id=25994) yourself, of course this means maintaining a copy of the support library yourself and not being able to use the native support, however that depends on how important this issue is for you.

    0 讨论(0)
  • 2021-02-01 16:11

    Okay so this is a bug which also is a problem for native library (not only support library).

    The only workaround I can suggest is to create your own back stack and then handle onBack with your own custom implementation setting the right animation as you go back through your own stack.

    0 讨论(0)
  • 2021-02-01 16:14

    You can use onCreateAnimation plus AnimationUtils for each fragment instead of transaction.setCustomAnimations(..). Also to skip animation during restoring, consider about boleean flag.

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        mIsRestoring = savedInstanceState != null;
        ...
    }
    
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        if (mIsRestoring) {
            mIsRestoring = false;
            return null;
        }
        if (enter) {
            return AnimationUtils.loadAnimation(getContext(), R.anim.enter_from_right);
        } else {
            return AnimationUtils.loadAnimation(getContext(), R.anim.exit_to_left);
        }
    }
    
    0 讨论(0)
  • 2021-02-01 16:18

    This bug is fixed few days ago in new support library 23.3.0. https://code.google.com/p/android/issues/detail?id=25994#c36

    0 讨论(0)
提交回复
热议问题