From 9a2454c59578c50057440134f1f525650985f07f Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Fri, 2 Jul 2021 14:06:22 +0800 Subject: [PATCH] Fix NPE caused by config change on a destroyed activity The config callback from view root may not know the state of activity. That may trigger a stale callback, so - Restore the null check when getting mSizeConfigurations. - Clear the callback if the activity will be removed. Bug: 192603207 Test: ActivityLifecycleTopResumedStateTests Change-Id: I17ea8fc2ba994f630d5ad7baa3de298d90a32827 --- core/java/android/app/ActivityThread.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 6505adccb46f7..7e0ccaef4f6b7 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -5430,6 +5430,12 @@ public final class ActivityThread extends ClientTransactionHandler // behave properly when activity is relaunching. r.window.clearContentView(); } else { + final ViewRootImpl viewRoot = v.getViewRootImpl(); + if (viewRoot != null) { + // Clear the callback to avoid the destroyed activity from receiving + // configuration changes that are no longer effective. + viewRoot.setActivityConfigCallback(null); + } wm.removeViewImmediate(v); } } @@ -5834,10 +5840,9 @@ public final class ActivityThread extends ClientTransactionHandler final boolean movedToDifferentDisplay = isDifferentDisplay(activity.getDisplayId(), displayId); - final SizeConfigurationBuckets buckets = getActivityClient(activityToken) - .mSizeConfigurations; + final ActivityClientRecord r = mActivities.get(activityToken); final int diff = diffPublicWithSizeBuckets(activity.mCurrentConfig, - newConfig, buckets); + newConfig, r != null ? r.mSizeConfigurations : null); final boolean hasPublicConfigChange = diff != 0; // TODO(b/173090263): Use diff instead after the improvement of AssetManager and // ResourcesImpl constructions.