编程语言
首页 > 编程语言> > java – 按下后退按钮时未加载新首选项

java – 按下后退按钮时未加载新首选项

作者:互联网

我有这个首选项类(下面)保存两个ListPreferences,但如果更改了ListPreferences并按下了后退按钮,除非重新启动应用程序,否则更改不会生效.我错过了什么?一直在寻找,但似乎无法找到适合或工作的答案.请帮忙.

    public class Preferences extends PreferenceActivity {

      @Override
      public void onCreate(Bundle savedInstanceState){
           super.onCreate(savedInstanceState);
           addPreferencesFromResource(R.xml.preferences);
           }

      @Override
      public void onPause() {
           super.onPause();
           }

      @Override
      public void onResume() {
           super.onResume();
           }
      }

应用代码

 public class Quotes extends Activity implements OnClickListener {

 ProgressDialog dialog;
 private WebView webview;

 @Override
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

      String q = SP.getString("appViewType","http://www.google.com");
      String c = SP.getString("appRefreshRate","20");

      webview = (WebView) findViewById(R.id.scroll);
      webview.getSettings().setJavaScriptEnabled(true);
      webview.setWebViewClient(new QuotesWebView(this));
      webview.loadUrl(q);

      ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
      timer.scheduleAtFixedRate(new Runnable() {

      @Override
      public void run() {
           webview.reload();
           }

      }, 10, Long.parseLong(c),TimeUnit.SECONDS);

      findViewById(R.id.refresh).setOnClickListener(this);
 }

      @Override
      public void onPause(){
           super.onPause();
           }

      @Override
      public void onResume(){
           super.onResume();
           }

      public void onClick(View v){
           switch(v.getId()){
                case R.id.refresh:
                webview.reload();
           break;
      }
 }


 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);

      MenuItem about = menu.getItem(0);
      about.setIntent(new Intent(this, About.class));

      MenuItem preferences = menu.getItem(1);
      preferences.setIntent(new Intent(this, Preferences.class));

      return true;

      }

 }   

解决方法:

当首选项活动完成时,您需要以某种方式重新加载您的首选项.我认为Dirol建议在onResume()而不是onCreate()中加载它们是非常好的;你试过吗?或者我也误解了这个问题.

在我自己的情况下,我使用startActivityForResult()启动了首选项活动,然后在活动结果回调中,我重新加载了首选项.

代码片段:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case MENU_PREFERENCES:
        Intent intent = new Intent().setClass(this, CalcPreferences.class);
        startActivityForResult(intent, MENU_PREFERENCES);
        break;
      default: return super.onOptionsItemSelected(item);
    }
    return true;
}

@Override
protected void onActivityResult(int req, int result, Intent data) {
    switch( req ) {
      case MENU_PREFERENCES:
        SharedPreferences sp =
          PreferenceManager.getDefaultSharedPreferences(this);
        updatePreferences(sp);
        break;
      default:
        super.onActivityResult(req, result, data);
        break;
    }
}

@Override
protected void updatePreferences(SharedPreferences sp) {
    super.updatePreferences(sp);
    keyclick = sp.getBoolean("keyclick", keyclick);
}

无论如何,这对我有用.我可能会尝试将我的updatePreferences()调用移动到onResume(),看看是否也能正常工作.

标签:onpause,preferenceactivity,android,java,preferences
来源: https://codeday.me/bug/20190826/1734915.html