diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java index 5a506193b8a17..f2bad6caf3e8a 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java @@ -1384,11 +1384,20 @@ public class PipTaskOrganizer implements ShellTaskOrganizer.TaskListener, final ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.0f); animator.setDuration(mCrossFadeAnimationDuration); animator.addUpdateListener(animation -> { - final float alpha = (float) animation.getAnimatedValue(); - final SurfaceControl.Transaction transaction = - mSurfaceControlTransactionFactory.getTransaction(); - transaction.setAlpha(surface, alpha); - transaction.apply(); + if (mState == State.UNDEFINED) { + // Could happen if onTaskVanished happens during the animation since we may have + // set a start delay on this animation. + Log.d(TAG, "Task vanished, skip fadeOutAndRemoveOverlay"); + animation.removeAllListeners(); + animation.removeAllUpdateListeners(); + animation.cancel(); + } else { + final float alpha = (float) animation.getAnimatedValue(); + final SurfaceControl.Transaction transaction = + mSurfaceControlTransactionFactory.getTransaction(); + transaction.setAlpha(surface, alpha); + transaction.apply(); + } }); animator.addListener(new AnimatorListenerAdapter() { @Override @@ -1401,6 +1410,10 @@ public class PipTaskOrganizer implements ShellTaskOrganizer.TaskListener, } private void removeContentOverlay(SurfaceControl surface, Runnable callback) { + if (mState == State.UNDEFINED) { + // Avoid double removal, which is fatal. + return; + } final SurfaceControl.Transaction tx = mSurfaceControlTransactionFactory.getTransaction(); tx.remove(surface);