问题
It updates front activity when I edit or just click ok on any input in preferences screen and than i must first hide front activity. It gives me zero on button and in preferences xml i gave right key and also defaultValue.
So .. scenario: i go to preferences screen, click to edit button text, just click ok, go back, its "0" on button, hide front activity, go back to front activity and theres is the right value.
I hope i included all necessary parts.
private SharedPreferences prefs;
private String mobileNumber;
private static final int MENU_PREFERENCES = Menu.FIRST;
private static final int SHOW_PREFERENCES = 1;
private void updateFromPreferences() {
mobileNumber = prefs.getString("mobileNumber", "0");
}
private void refreshAplication(){
button.setText(mobileNumber);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
updateFromPreferences();
refreshAplication();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case (MENU_PREFERENCES): {
Intent i = new Intent(this, Preferences.class);
startActivityForResult(i, SHOW_PREFERENCES);
return true;
}
}
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SHOW_PREFERENCES)
if (resultCode == Activity.RESULT_OK) {
updateFromPreferences();
refreshAplication();
}
}
public class Preferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.userpreferences);
}
}
回答1:
Call refreshAplication();
in onResume()
.
回答2:
You should be following this :
example
来源:https://stackoverflow.com/questions/8954498/wont-update-from-preferences