material-components / material-components/material-components-android

Unable to apply DynamicColors in conjunction with AppCompatDelegate.setDefaultNightMode()

Open
#3,366 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug Feature: Color
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

Material version: 1.9.0-rc01
Tested on Pixel 4a, Android 13

My app theme is a custom theme whose parent is Theme.Material3.DynamicColors.DayNight

I can use the DynamicColors api just fine to apply it globally in my Application's onCreate():

DynamicColors.applyToActivitiesIfAvailable(this, new DynamicColorsOptions.Builder()build());

With this, the dynamic colors (Material You) are applied.

However, If I use AppCompatDelegate.setDefaultNightMode() (called before of after the applyToActivitiesIfAvailable() call, it does not matter) to force dark mode on or off (depending of app setting),
the dynamic colors are not applied and the theme is using whatever default colors are configured for non dynamic mode (purple in my case, the default M3 color scheme, since I did not define custom colors in my theme yet).

After some digging into it, I see that a call AppCompatDelegate.setDefaultNightMode() results in some heavy stuff being done in the activity's attachbaseContext() which is called prior the DynamicColors's onActivityPreCreated() hook.

Apparently these 2 things conflict with each other. Or am I doing something wrong ? AppCompatDelegate.setDefaultNightMode() is otherwise working fine outside of being used with DynamycColors.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing DynamicColors.applyToActivitiesIfAvailable from Application.onCreate through its onActivityPreCreated hook, alongside AppCompatDelegate.setDefaultNightMode and the activity attachBaseContext path described in the report. Reproduce the issue on Android 13 with Material 1.9.0-rc01, then verify that forcing night mode still preserves dynamic colors.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.