From ed841cbdd709d3874506510fd774d961be711f31 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Thu, 11 Aug 2016 11:00:23 -0700 Subject: [PATCH] Make View#isTemporarilyDetached() less confusing In Android N, View#isTemporarilyDetached() returns true if it is called when the same View instance is handling View#onFinishTemporaryDetach(). Returning true there is, however, sometimes confusing, especially scenarios like the following case: @Override public void onFinishTemporaryDetach() { doSomeRestoringWorks(); } private void doSomeRestoringWorks() { doSomething(); } private doSomething() { if (isTemporarilyDetached()) { // As of N, we hit here if this is called as a result of // View#onFinishTemporaryDetach(). } else { // but is the logic here is more likely to be appropriate // when handling View#onFinishTemporaryDetach()? } } What this CL aims to do is to let View#isTemporarilyDetached() return false when it is called while handling View#onFinishTemporaryDetach(), because it should make View#onFinishTemporaryDetach() more useful. Regarding the app compatibility, View#isTemporarilyDetached() was added in API 24 hence the impact on this change is still limited. Bug: 30791718 Change-Id: If384da9f9e6ff849598896901626fd021bae5cda --- core/java/android/view/View.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index e7553ec943ec..475b4c15b38e 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -830,7 +830,6 @@ public class View implements Drawable.Callback, KeyEvent.Callback, */ protected static boolean sPreserveMarginParamsInLayoutParamConversion; - /** * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when * calling setFlags. @@ -9852,6 +9851,18 @@ public class View implements Drawable.Callback, KeyEvent.Callback, } /** + * Tells whether the {@link View} is in the state between {@link #onStartTemporaryDetach()} + * and {@link #onFinishTemporaryDetach()}. + * + *

This method always returns {@code true} when called directly or indirectly from + * {@link #onStartTemporaryDetach()}. The return value when called directly or indirectly from + * {@link #onFinishTemporaryDetach()}, however, depends on the OS version. + *

+ *

+ * * @return {@code true} when the View is in the state between {@link #onStartTemporaryDetach()} * and {@link #onFinishTemporaryDetach()}. */ @@ -9886,8 +9897,8 @@ public class View implements Drawable.Callback, KeyEvent.Callback, */ @CallSuper public void dispatchFinishTemporaryDetach() { - onFinishTemporaryDetach(); mPrivateFlags3 &= ~PFLAG3_TEMPORARY_DETACH; + onFinishTemporaryDetach(); if (hasWindowFocus() && hasFocus()) { InputMethodManager.getInstance().focusIn(this); } -- 2.11.0