Does anyone know if android supports sharing the same sharedpreference across multiple Android Modules compiled in one project?
i have two shared preferences and currently when i try to access some data from a shared prefence outside of the current module, it doesnt work and creates a new sharedPrefence instead
example
Module one:
mSharedPreferences = context.getSharedPreferences(
"pref", Context.MODE_PRIVATE);
Module Two:
mSharedPreferences = context.getSharedPreferences(
"pref", Context.MODE_PRIVATE);
It creates two preference file instead of one where both modules can share the data
You can create a common :core
module and add all your dependencies in that module.
Implement :core
module in all your other modules.
Place you PrefHelpers
class in :core
module to access them from any module or
you could configure sharedpreference using common package name can be obtained using BuildConfig.APPLICATION_ID
.
mSharedPreferences = context.getSharedPreferences(
"pref", Context.MODE_PRIVATE);
can be replaced with
mSharedPreferences = context.getSharedPreferences(
BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
You can use the mode as Context.MODE_PRIVATE
itself.
Make sure your BuildConfig
is from your :core
module.
If it resolves your issue comment back.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With