Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daylight theme is not updated when app is launched #32

Open
JulienDev opened this issue Nov 4, 2020 · 14 comments · May be fixed by #33 or #45
Open

Daylight theme is not updated when app is launched #32

JulienDev opened this issue Nov 4, 2020 · 14 comments · May be fixed by #33 or #45

Comments

@JulienDev
Copy link

Hello, me again :)

I've noticed that by using the library, when the app is already launched and the the dark theme is switched on or off, the app keep his current theme until the app killed and launched again. Do you know a way to "refresh" the UI when the setting is changed?

In the meantime I'd like to thank you a lot for your awesome library that helps a lot !

@gunhansancar
Copy link
Member

In the demo application, I have added the Light-Dark theme and it works when you switch the theme without any problems. Can you check that please if your case is the same?

@JulienDev
Copy link
Author

JulienDev commented Nov 5, 2020

I've just tried the sample app and I can confirm it suffers the same issue. Like me, you are settings AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM) when App.onCreate() is called.

When the dark mode is changed on the system wide, and the app is launched, the activity theme is not updated. When I don't use the library (replace LocaleAwareApplication by Application) it's working well.

@gunhansancar
Copy link
Member

Just for the last sentence When I don't use the library (replace LocaleAwareApplication by Activity) it's working well. what do you mean? LocaleAwareApplication by Activity? You mean by Application?

@JulienDev
Copy link
Author

JulienDev commented Nov 5, 2020

Yep sorry (I've updated my comment)

@gunhansancar
Copy link
Member

So the case is that the app is running and backgrounded, then you go the system settings and make the theme Dark there, and you restore the app from the recents, and the app couldn't pick up the new configuration change ?

@JulienDev
Copy link
Author

I've recorded two gifs to make it easier to understand :)

The first one use your library :
with_library

The second one doesn't use your library :
without_library

@gunhansancar
Copy link
Member

How the language is changing without using the library in the second gif :) ?

OK, I will address the issue soon. Also feel free to send a PR if you know the solution already.

@JulienDev
Copy link
Author

The language is not changing without the library haha, that was just to illustrate how it works without the library ^^

I'd love to help you by opening a PR but I don't have enough knowledges on the subject to fix it :/

@c0dehunter
Copy link

c0dehunter commented Mar 24, 2021

I can confirm this issue. Actually I was using another library for Locale changing and it has the same problem: franmontiel/LocaleChanger#20

@thubalek
Copy link

thubalek commented Dec 2, 2021

I can confirm this issue two.

@thubalek
Copy link

thubalek commented Dec 2, 2021

@gunhansancar

I found this comment in AppCompatDelegateImpl.java, method public Context attachBaseContext2(@NonNull final Context baseContext):

        // This is a tricky method. Here are some things to avoid:
        // 1. Don't modify the configuration of the Application context. All changes should remain
        //    local to the Activity to avoid conflicting with other Activities and internal logic.
        // 2. Don't use createConfigurationContext() with Robolectric because Robolectric relies on
        //    method overrides.
        // 3. Don't use createConfigurationContext() unless you're able to retain the base context's
        //    theme stack. Not the last theme applied -- the entire stack of applied themes.

As you use createConfigurationContext in LocaleHelper.updateContextResources I'm afraid it can be root cause of the problem.

@thubalek
Copy link

thubalek commented Dec 2, 2021

BTW: I implemented super stupid fix. In class LocaleHelperAppCompatDelegate method attachBaseContext2 looks like this:

override fun attachBaseContext2(originalContext: Context): Context {
        val originalUiMode = originalContext.resources.configuration.uiMode
        val appCompatDelegateContext = super.attachBaseContext2(originalContext)
        val superDelegateContext = superDelegate.attachBaseContext2(appCompatDelegateContext)
        val wrappedContext = wrap(superDelegateContext)
        val fixedWrappedContext = wrappedContext.createConfigurationContext(wrappedContext.resources.configuration.apply {
            this.uiMode = originalUiMode
        })
        return fixedWrappedContext
    }

I don't think the fix is right (it looks stupid to me) but I may fix the problem. And introduce new-hard-to-find bugs.

@thubalek
Copy link

thubalek commented Dec 6, 2021

Hmm, fix above is not correct solution.

thubalek added a commit to thubalek/locale-helper-android that referenced this issue Dec 6, 2021
thubalek added a commit to thubalek/locale-helper-android that referenced this issue Dec 6, 2021
Code needs polishing
thubalek added a commit to thubalek/locale-helper-android that referenced this issue Dec 7, 2021
thubalek added a commit to thubalek/locale-helper-android that referenced this issue Dec 7, 2021
@thubalek thubalek linked a pull request Dec 7, 2021 that will close this issue
thubalek added a commit to thubalek/locale-helper-android that referenced this issue Dec 7, 2021
@JulienDev
Copy link
Author

That's a crazy issue... If you know a library that handle correctly this theme change let me know :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants