From dff365ef4dc61239fac70953b631e92972a9f41f Mon Sep 17 00:00:00 2001 From: daqi Date: Wed, 19 Sep 2018 15:51:22 +0800 Subject: [PATCH] Set InputMethoMananger#mCurRootView to null when window dismissed InputMethodManager#sInstance is a long live static object so we have to set its field with right value, otherwise any object referenced by it cannot be gc including potential activity context. Now InputMethodManager#mCurRootView is set to null in InputMethodManager#onPreWindowFocus which is invoked when app's ViewRootImpl received ViewRootImpl#W#windowfocusChanged from WMS. However, in the ViewRootImpl#W#windowfocusChanged, mViewAncestor is a weak reference which get() may returns null sometimes. One scenario is the ViewRootImpl#W#windowfocusChanged is called after ActivityThread#handleDestroyActivity. The activity is destroyed and its ViewRootImpl get GC'd. Then InputMethodManager#onPreWindowFocus won't get called and InputMethodManager#mCurRootView won't be set to null. And it is a proper time to set InputMethodManager#mCurRootView to null when the window it served dismissed. Fix: 116078227 Test: Break at ActivityThread#handleDestroyActivity and ViewRootImpl#W#windowfocusChanged Change-Id: I8fabb30f14bcb2cd7019e29b6642b4562d49d248 Signed-off-by: daqi --- core/java/android/view/inputmethod/InputMethodManager.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java index 53b224c36cb29..819cf440667b2 100644 --- a/core/java/android/view/inputmethod/InputMethodManager.java +++ b/core/java/android/view/inputmethod/InputMethodManager.java @@ -1420,6 +1420,10 @@ public final class InputMethodManager { mServedView.getWindowToken() == appWindowToken) { finishInputLocked(); } + if (mCurRootView != null && + mCurRootView.getWindowToken() == appWindowToken) { + mCurRootView = null; + } } }