I have a trouble with SharedPreferences
when I try read data from remote service.
First, In SplashScreenActivity
, my app start
and bind
my RemoteService
Second, SplashScreenActivity
start MainActivity
, this MainActivity
will save configuration info to SharedPreferences
:
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString("ABC_KEY", "abc");
editor.apply();
Then, MainActivity
will send Broadcast
:
sendBroadcast(new Intent("PLZ_READ_DATA"));
Finally, in RemoteService
receive above Broadcast
, it will read data from SharedPreferences
:
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String str = mSharedPreferences.getString("ABC_KEY", null);
The problem is str
is always null
.
What is the magic here? Can anyone explain it and give a solution for read data from SharedPreferences
on this case.