Android Q offers a new Dark theme.
Does LottieAnimationView support this behavior?
I put json on res/raw for light mode, res/raw-night for dark mode.
and in xml uses app:lottie_rawRes="@raw/sample".
I change theme to light or dark, it still plays the same json file.
I also use animationView.setAnimation(R.raw.sample), it still plays the same json file.
I'm pretty sure the json is changed(I dump the content) when theme for light to dark or dark to light mode.
so is anyone has the experience on this?
thanks.
I have a same issue.
I think, LottieComposition is recognized as the same object.
So I am writing a temporary code.
Removed Codes
Is there a good alternative? 馃
I have a mistake. :sob:
The following code is NOT responding on configuration change.
private static String rawResCacheKey(@RawRes int resId) {
return "rawRes_" + resId;
}
So different IDs use is only solution.
favoriteButton.setAnimation(if (isDarkTheme) {
R.raw.lottie_animation_night
} else {
R.raw.lottie_animation_day
})
But, native supports is a better solution. 馃憦 馃憦 馃憦
@lantimes @fornewid I will look into adding native support for this! 馃槃
Checked with 3.1.0-SNAPSHOT and still "day" resources are picked in night mode. From what I was debugging, isNightMode() returns true when calling rawResCacheKey() in fromRawRes(), but false when generating cache key once again in fromRawResSync, don't know why 馃
Android Q Beta 6, Pixel 2 XL
@plnice Hmm, any chance you have a little more time to debug? I don't want to ship this if it's broken but I don't have time right now to take a look.
@gpeal sure, will have a look at this more tomorrow. Right now I suspect that the wrong uiMode value is returned when obtaining it from application context (which is passed to the fromRawResSync(), in fromRawRes() passed context attribute is used), but will confirm this when I jump into the code and make sure that's the issue.
@gpeal after looking into this, I found out that the application context always incorrectly returns UI_MODE_NIGHT_NO. Activity has the correct information. Also, when switching between default and night mode when the application is running, application context can return wrong resource ("day" resource after user switched from day to night and vice versa).
To fix this, I switched to consistently using the original context (instead of using the application context ). To not leak the Activity, I'm using the WeakReference. If the Activity is not reachable, LottieResult with NPE will be returned - hope this will be handled gracefully and the app won't crash?
PR: #1361
Most helpful comment
@lantimes @fornewid I will look into adding native support for this! 馃槃