在动画之后提交FragmentTransaction会导致IllegalStateException

时间:2017-11-06 16:03:03

标签: android android-fragments android-animation

我在onCreate事件中调用了以下方法:

private void runAnimations() {
        new Handler()
                .postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        final ImageView logo = findViewById(R.id.fullscreen_content);
                        ValueAnimator animator = ValueAnimator.ofInt(0, 180);
                        animator.setInterpolator(new DecelerateInterpolator());
                        animator.setDuration(2500);
                        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                final int progress = (int) animation.getAnimatedValue();
                                logo.setScrollY(progress * 2);
                            }
                        });

                        animator.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator animation) {
                                animateLoginForm();
                            }
                        });
                        animator.start();
                    }
                }, 1000);
    }

    private void animateLoginForm() {
        FragmentManager fragmentManager = getSupportFragmentManager();

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);

        LoginFragment loginFragment = LoginFragment.newInstance();
        transaction.replace(R.id.login_fragment_container, loginFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

但它导致以下异常:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
                                                                       at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2044)
                                                                       at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2067)
                                                                       at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:680)
                                                                       at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:634)
                                                                       at com.mygroup.holder.activities.LoginActivity.animateLoginForm(LoginActivity.java:158)
                                                                       at com.mygroup.holder.activities.LoginActivity.access$300(LoginActivity.java:30)
                                                                       at com.mygroup.holder.activities.LoginActivity$4$2.onAnimationEnd(LoginActivity.java:141)
                                                                       at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1239)
                                                                       at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:766)
                                                                       at android.animation.ValueAnimator$AnimationHandler$1.run(ValueAnimator.java:801)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

由于animateLoginForm()是在匿名类的异步方法中调用的,因此我无法保证在onSaveInstanceState事件之前调用它。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

能够通过加载片段并在运行动画之前提交事务来解决它,然后我只需要设置它是不可见的,在动画之后我将它设置为可见。

body