I want to seed my app's default preferences from a build variable, so I cannot use the XML-based preference defaults.
In my MainActivity
, I have this method:
/**
* Set the default values programmatically
*/
protected void initPreferences() {
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
if (sharedPref.getString("elasticsearch_url", "").equals("")) {
editor.putString("elasticsearch_url", Env.elasticsearchTestUrl);
}
if (sharedPref.getString("elasticsearch_user", "").equals("")) {
editor.putString("elasticsearch_user", Env.elasticsearchTestUser);
}
if (sharedPref.getString("elasticsearch_password", "").equals("")) {
editor.putString("elasticsearch_password", Env.elasticsearchTestPassword);
}
if (sharedPref.getString("elasticsearch_index", "").equals("")) {
editor.putString("elasticsearch_index", Env.elasticsearchTestIndex);
}
editor.commit();
Log.d(TAG, "Settings: " + sharedPref.getAll());
}
I call this within onCreate
directly after the app starts. The log shows the settings being saved fine.
Now, I have a second activity, an automatically-generated SettingsActivity
, which looks like this:
package com.aveq.testandroidpacketcapture;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsActivity extends AppCompatActivity {
private static final String TAG = "SettingsActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(false);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Log.d(TAG, "Setting preferences from XML resource ...");
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
Whenever I start this activity via:
Intent intent = new Intent(this, SettingsActivity.class);
startActivityForResult(intent, 0);
the settings activity opens, but the values there are empty:
The XML based on which the preferences are loaded looks like this:
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:title="Elasticsearch">
<EditTextPreference
app:key="elasticsearch_url"
app:title="Elasticsearch URL"
app:useSimpleSummaryProvider="true"/>
<EditTextPreference
app:key="elasticsearch_user"
app:title="Elasticsearch username"
app:useSimpleSummaryProvider="true"/>
<EditTextPreference
app:key="elasticsearch_password"
app:title="Elasticsearch password"
app:useSimpleSummaryProvider="true"/>
<EditTextPreference
app:key="elasticsearch_index"
app:title="Elasticsearch index"
app:useSimpleSummaryProvider="true"/>
</PreferenceCategory>
</PreferenceScreen>
As far as I can tell, all the keys match up. When I log the shared preferences in the onCreate
method of the settings activity, they are correct, so why are my values not loaded?
When using a PreferenceScreen the SharedPreferences get saved to default shared preferences so you need to grab them from there with this
PreferenceManager.getDefaultSharedPreferences(this)
vs pulling them from private context
getPreferences(Context.MODE_PRIVATE)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.