Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android project with different modules(sharedPreferences)

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

like image 431
Jonathan Avatar asked May 27 '16 10:05

Jonathan


1 Answers

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.

like image 51
Perusu Droid Avatar answered Oct 07 '22 01:10

Perusu Droid