From efca8e901aae2096ddedc1c0037a968d1a2c9979 Mon Sep 17 00:00:00 2001 From: Yoshihide Sogawa <12043975+YoshihideSogawa@users.noreply.github.com> Date: Tue, 2 Oct 2018 17:14:02 +0900 Subject: [PATCH] Fix IllegalArgumentException - it only happens with Android 8. --- .../floatingview/FloatingViewManager.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/jp/co/recruit_lifestyle/android/floatingview/FloatingViewManager.java b/library/src/main/java/jp/co/recruit_lifestyle/android/floatingview/FloatingViewManager.java index d7d995c..d70a898 100644 --- a/library/src/main/java/jp/co/recruit_lifestyle/android/floatingview/FloatingViewManager.java +++ b/library/src/main/java/jp/co/recruit_lifestyle/android/floatingview/FloatingViewManager.java @@ -26,6 +26,7 @@ import android.support.annotation.DrawableRes; import android.support.annotation.IntDef; import android.support.annotation.NonNull; +import android.support.v4.view.ViewCompat; import android.util.DisplayMetrics; import android.view.DisplayCutout; import android.view.HapticFeedbackConstants; @@ -544,7 +545,7 @@ public void addViewToWindow(View view, Options options) { mWindowManager.addView(mFullscreenObserverView, mFullscreenObserverView.getWindowLayoutParams()); mTargetFloatingView = floatingView; } else { - mWindowManager.removeViewImmediate(mTrashView); + removeViewImmediate(mTrashView); } // 必ずトップに来て欲しいので毎回貼り付け mWindowManager.addView(mTrashView, mTrashView.getWindowLayoutParams()); @@ -559,7 +560,7 @@ private void removeViewToWindow(FloatingView floatingView) { final int matchIndex = mFloatingViewList.indexOf(floatingView); // 見つかった場合は表示とリストから削除 if (matchIndex != -1) { - mWindowManager.removeViewImmediate(floatingView); + removeViewImmediate(floatingView); mFloatingViewList.remove(matchIndex); } @@ -576,17 +577,29 @@ private void removeViewToWindow(FloatingView floatingView) { * ViewをWindowから全て取り外します。 */ public void removeAllViewToWindow() { - mWindowManager.removeViewImmediate(mFullscreenObserverView); - mWindowManager.removeViewImmediate(mTrashView); + removeViewImmediate(mFullscreenObserverView); + removeViewImmediate(mTrashView); // FloatingViewの削除 final int size = mFloatingViewList.size(); for (int i = 0; i < size; i++) { final FloatingView floatingView = mFloatingViewList.get(i); - mWindowManager.removeViewImmediate(floatingView); + removeViewImmediate(floatingView); } mFloatingViewList.clear(); } + /** + * Safely remove the View (issue #89) + * + * @param view {@link View} + */ + private void removeViewImmediate(View view) { + if (!ViewCompat.isAttachedToWindow(view)) { + return; + } + mWindowManager.removeViewImmediate(view); + } + /** * Find the safe area of DisplayCutout. *