I’ve logged maybe 6 hours so far fighting with Android preferences, including reviewing and trying many stackoverflow suggestions. My latest problem is that changes I make from my preferences activity aren’t carrying over to my main activity. I have distilled the code.
Main activity:
public class MainActivity extends Activity {
/*...*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*...*/
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.menu_settings:
startActivity(new Intent(this, PrefsActivity.class));
return true;
}
return false;
}
@Override
public void onStart()
{
super.onStart();
SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
int somePrefValue = prefs.getInt("somePrefKey", 1);
/*...*/
}
}
Preferences activity:
public class PrefsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_prefs);
}
}
Preferences resource:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Some Category">
<EditTextPreference
android:key="somePrefKey"
android:title="Some Preference"
android:defaultValue="1"
android:numeric="integer" />
</PreferenceCategory>
</PreferenceScreen>
In case it matters, I’m having this trouble in 4.1, both in the emulator and on a Nexus 7.
<VENT>For the record, here are the battles I have so far fought (and won) with preferences:
- Where the XML preferences file goes in the Eclipse tree and what to
call it. - How to force the preferences activity to restrict values to
integers or floats. - That specifying a preference value as an
integer or float in the XML does not make the preference an
integer or float — it’s still a string. - That despite several methods
taking the name of the shared preferences file, the only method that
will actually load the file is addPreferencesFromResource(), only
from PreferenceActivity. I have to code the defaults redundantly in
the XML and in the first prefs getters, unless I parse the XML.
I had these problems despite reading the official Android documentation for preferences and despite reading the chapters on using preferences in three different books. Android documentation is generally pretty good, but considering my (continuing) trouble and the vast number of stackoverflow questions about this, I think this part needs some work.</VENT>
Thank you for any help you can provide with my latest problem.
P.S. I realize that the non-fragment approach is now deprecated, but I’m new to Android and have a tight deadline for producing a prototype that need not run on anything but my Nexus 7 tablet. I’d prefer it simple, though it’s now looking like I didn’t save any time.
You seem to be using a non-default SharedPreferences in your main activity
"app_prefs", but your PreferenceActivity doesn’t reference this, and is probably storing to the default SharedPreferences.You should either:
Use the default SharedPreferences in the Main activity, instead of a named one: (also moving to onResume as suggested by louielouie)
or
Tell your PreferenceActivity the name of the SharedPreferences you want to use: