I'm using an activity called SettingsActivity
that extends PreferenceFragmentCompat
. When the application starts for the very first time MainActivity
, I try to get the preferences through getDefaultSharedPreferences(getApplicationContext())
but without success because they are only loaded when I start the Settings activity.
To prove this, I checked the file shared_preferences.xml and it's filled only when I open the SettingsActivity
.
In this sense, how can I force the loading of the preferences in the MainActivity
?
MainActivity
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map<String, ?> allPreferences = getDefaultSharedPreferences(MainActivity.applicationContext).getAll();
Log.d(Class.forName(), allPreferences.isEmpty()); //prints true
Intent intent_settings = new Intent(activityContext, SettingsActivity.class);
startActivity(intent_settings);
}
}
SettingsActivity
public class SettingsActivity extends PreferenceFragmentCompat {
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setPreferencesFromResource(R.xml.preferences, rootKey);
Map<String, ?> allPreferences = getPreferenceManager().getSharedPreferences().getAll();
Log.d(Class.forName, allPreference.isEmpty()); // prints false
}
}