Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Not working in combination with dagger hilt #128

Open
Berki2021 opened this issue Dec 30, 2021 · 3 comments
Open

Not working in combination with dagger hilt #128

Berki2021 opened this issue Dec 30, 2021 · 3 comments

Comments

@Berki2021
Copy link

I wanted to use this library in combination with dagger-hilt, but that does not seem to work. To be more specific:

There is a dagger-hilt annotation called @ApplicationContext, with which we can inject the application context inside every possible non-android-class. Here is an example:

class MyRepository(
    @ApplicationContext private val context: Context
) {
   fun getData() =  myService.collection(context.getString(R.string.stuff)).get().await()
}

Now comes the problem: When switching languages, this ApplicationContext does not seem to update. For example, let's say we have two versions of the above string:

German "R.string.stuff" -> Hallo
English "R.string.stuff" -> Hello

When you now change the language from german to english in Fragment with (requireActivity as LocaleAwareCompatActivity).updateLocal(Locale.EN), then the following happens:

Calling requireContext().getString(R.string.stuff) in Fragment gives "Hello" (language was updated correctly). But inside my Repository "R.string.stuff" is still "Hallo" (language was not updated correctly)

@akexorcist
Copy link
Owner

I have sample code for Dagger Hilt implementation

See

ApplicationContext was injected with @ApplicationContext into StoryProvider and also change the language from HiltFragmentWithViewModel (Fragment). This work properly (see HiltDependencyInjectionTest UI test for this implementation)

Feel free to tell me more information if part of your code that different from my sample code

@Berki2021
Copy link
Author

Berki2021 commented Dec 30, 2021

I have sample code for Dagger Hilt implementation

See

* [HiltFragmentWithViewModel](https://github.com/akexorcist/Localization/blob/master/app/src/main/java/com/akexorcist/localizationapp/hilt/HiltFragmentWithViewModel.kt)

* [StoryProvider](https://github.com/akexorcist/Localization/blob/master/app/src/main/java/com/akexorcist/localizationapp/hilt/StoryProvider.kt)

ApplicationContext was injected with @ApplicationContext into StoryProvider and also change the language from HiltFragmentWithViewModel (Fragment). This work properly (see HiltDependencyInjectionTest UI test for this implementation)

Feel free to tell me more information if part of your code that different from my sample code

OK I will test this later and tell you if it worked.

EDIT:

I think I know where the problem lies. I didn't test this, but what about classes that live as Singletons and have an applicationContext injected into them? Classes such as Repositories annotated with @Singleton.

I observed the following: When I change the language, the applicationContext inside my singleton classes doesn't get updated. But when I now restart the app, the applicationContext is updated because the app has been newly created.

Might this be the Issue @akexorcist ?

@akexorcist
Copy link
Owner

Since AndroidX AppCompat has per-app language preferences for backward compatibility. Please migrate this library to AndroidX for more stability, compatibility, and longer support from Google team.

See Migrate to AndroidX guide

Thank you for your support

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants