I created a simple class "User" that has an attribute String username
. I'm assuming that since Android uses Java, I can create custom classes and it'll use them accordingly. In my SettingsActivity.java, I have:
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
SharedPreferences sharedPref = getPreferenceScreen().getSharedPreferences();
EditTextPreference editTextPref = (EditTextPreference) findPreference("username");
editTextPref.setSummary(sharedPref.getString("username", "Default"));
//sharedPref.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
// Get current summary
Preference pref = findPreference(arg1);
if(pref instanceof EditTextPreference){
EditTextPreference etp = (EditTextPreference) pref;
pref.setSummary(etp.getText());
MainMenu.user.setName(etp.getText()); //This is where I try to update my class
}
}
As of right now, whenever I change the value of my EditTextPreference
widget, it doesn't update the public static user object located in MainMenu
. Also, a followup question - since Android saves the preferences with each instance of app launch, how would I update my User.username
String on startup?
In onPostCreate
remove the comment //
sharedPref.registerOnSharedPreferenceChangeListener(this);