won't update from Preferences

左心房为你撑大大i 提交于 2019-12-25 04:21:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!