From c6f220a503db0bbbd202f81494bce4d9d0463e8e Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Tue, 11 Aug 2015 16:35:09 -0400 Subject: [PATCH] Pass handling context when invoking resolved click handler Bug: 23101112 Change-Id: I7d09487ad2c751cd9133fc39ea814f18424b8519 --- core/java/android/view/View.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 17a9741fcdb64..a4925184f1814 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -4437,7 +4437,8 @@ public class View implements Drawable.Callback, KeyEvent.Callback, private final View mHostView; private final String mMethodName; - private Method mMethod; + private Method mResolvedMethod; + private Context mResolvedContext; public DeclaredOnClickListener(@NonNull View hostView, @NonNull String methodName) { mHostView = hostView; @@ -4446,12 +4447,12 @@ public class View implements Drawable.Callback, KeyEvent.Callback, @Override public void onClick(@NonNull View v) { - if (mMethod == null) { - mMethod = resolveMethod(mHostView.getContext(), mMethodName); + if (mResolvedMethod == null) { + resolveMethod(mHostView.getContext(), mMethodName); } try { - mMethod.invoke(mHostView.getContext(), v); + mResolvedMethod.invoke(mResolvedContext, v); } catch (IllegalAccessException e) { throw new IllegalStateException( "Could not execute non-public method for android:onClick", e); @@ -4462,11 +4463,16 @@ public class View implements Drawable.Callback, KeyEvent.Callback, } @NonNull - private Method resolveMethod(@Nullable Context context, @NonNull String name) { + private void resolveMethod(@Nullable Context context, @NonNull String name) { while (context != null) { try { if (!context.isRestricted()) { - return context.getClass().getMethod(mMethodName, View.class); + final Method method = context.getClass().getMethod(mMethodName, View.class); + if (method != null) { + mResolvedMethod = method; + mResolvedContext = context; + return; + } } } catch (NoSuchMethodException e) { // Failed to find method, keep searching up the hierarchy.