From 39ef5efaa555138cdd428f9e91084f2c362af706 Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Tue, 11 Oct 2022 15:12:51 +0800 Subject: [PATCH] Update layout direction if ViewRoot is preserved Otherwise if the language of the configuration is changed between RTL and LTR, the layout direction may still keep the previous direction after calling recreate(). Bug: 250868428 Test: The layout direction will change with below sample: Configuration config = getResources().getConfiguration(); configconfiguration.setLocales(LocaleList.forLanguageTags("ar")); getResources().updateConfiguration(configuconfigration, null); recreate(); Change-Id: Iad405bf02b2f19cb3aa8cbc21f9bca67d8a53c30 --- core/java/android/view/ViewRootImpl.java | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 91f36bf2c44ef..4522c0dcf719b 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -1102,7 +1102,7 @@ public final class ViewRootImpl implements ViewParent, // Update the last resource config in case the resource configuration was changed while // activity relaunched. - mLastConfigurationFromResources.setTo(getConfiguration()); + updateLastConfigurationFromResources(getConfiguration()); } private Configuration getConfiguration() { @@ -5396,13 +5396,7 @@ public final class ViewRootImpl implements ViewParent, // Update the display with new DisplayAdjustments. updateInternalDisplay(mDisplay.getDisplayId(), localResources); - final int lastLayoutDirection = mLastConfigurationFromResources.getLayoutDirection(); - final int currentLayoutDirection = config.getLayoutDirection(); - mLastConfigurationFromResources.setTo(config); - if (lastLayoutDirection != currentLayoutDirection - && mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) { - mView.setLayoutDirection(currentLayoutDirection); - } + updateLastConfigurationFromResources(config); mView.dispatchConfigurationChanged(config); // We could have gotten this {@link Configuration} update after we called @@ -5416,6 +5410,17 @@ public final class ViewRootImpl implements ViewParent, updateForceDarkMode(); } + private void updateLastConfigurationFromResources(Configuration resConfig) { + final int lastLayoutDirection = mLastConfigurationFromResources.getLayoutDirection(); + final int currentLayoutDirection = resConfig.getLayoutDirection(); + mLastConfigurationFromResources.setTo(resConfig); + // Update layout direction in case the language or screen layout is changed. + if (lastLayoutDirection != currentLayoutDirection && mView != null + && mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) { + mView.setLayoutDirection(currentLayoutDirection); + } + } + /** * Return true if child is an ancestor of parent, (or equal to the parent). */