From cc2a1d48a0e4134823c940291f5db6fd9f2ae9b3 Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Fri, 1 May 2015 11:28:37 -0700 Subject: Allow ContextImpl.setTheme(int) to be called after getTheme() Previously the theme could only be set if it had never been obtained, which was inconsistent with the documentation for Context.setTheme() and the behavior of ContextThemeWrapper. Also prevents double-apply of themes in setTheme(). Bug: 20756250 Bug: 20230451 Change-Id: I2566b3e4546cd1c68db79f10f01a5a1256fd439c --- core/java/android/app/ContextImpl.java | 24 ++++++++++++++++++------ core/java/android/view/ContextThemeWrapper.java | 9 ++++++--- 2 files changed, 24 insertions(+), 9 deletions(-) (limited to 'core') diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 81a78f6..cb20cb8 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -237,8 +237,11 @@ class ContextImpl extends Context { } @Override - public void setTheme(int resid) { - mThemeResource = resid; + public void setTheme(int resId) { + if (mThemeResource != resId) { + mThemeResource = resId; + initializeTheme(); + } } @Override @@ -248,13 +251,22 @@ class ContextImpl extends Context { @Override public Resources.Theme getTheme() { + if (mTheme != null) { + return mTheme; + } + + mThemeResource = Resources.selectDefaultTheme(mThemeResource, + getOuterContext().getApplicationInfo().targetSdkVersion); + initializeTheme(); + + return mTheme; + } + + private void initializeTheme() { if (mTheme == null) { - mThemeResource = Resources.selectDefaultTheme(mThemeResource, - getOuterContext().getApplicationInfo().targetSdkVersion); mTheme = mResources.newTheme(); - mTheme.applyStyle(mThemeResource, true); } - return mTheme; + mTheme.applyStyle(mThemeResource, true); } @Override diff --git a/core/java/android/view/ContextThemeWrapper.java b/core/java/android/view/ContextThemeWrapper.java index 9047b1d..ce50091 100644 --- a/core/java/android/view/ContextThemeWrapper.java +++ b/core/java/android/view/ContextThemeWrapper.java @@ -87,9 +87,12 @@ public class ContextThemeWrapper extends ContextWrapper { } } - @Override public void setTheme(int resid) { - mThemeResource = resid; - initializeTheme(); + @Override + public void setTheme(int resid) { + if (mThemeResource != resid) { + mThemeResource = resid; + initializeTheme(); + } } /** @hide */ -- cgit v1.1