Android - 在用户访问 PreferenceActivity 后确定 Preference 是否已更改

标签 android asynchronous preferenceactivity

我正在制作一款相当基础的游戏。在我的偏好中,有一个选项可以更改难度设置。我希望能够以某种方式在调用它的主要 Activity 中感知他们是否改变了难度。 (然后重启游戏)

我遇到了困难,因为偏好 Activity 是如何异步处理的。如果我添加逻辑来检查将 Intent 发送到我的 PreferenceActivity 之前和之后的值(从菜单选择),它真的不起作用......

关于如何序列化通常异步处理的代码块,或者知道如何感知偏好更改的想法,有人能给我指明正确的方向吗?有没有隐藏在某处的监听器类?

为了后代,这里是处理 Intent 的代码以及我是如何失败的。 ( fragment ,来自内部菜单 onOptionsItemSelected,内部开关)

case R.id.menuOptions:
    String currentDifficulty = preferences.getString("difficulty","problem!");
    Intent i = new Intent(this, prefs.class);
    startActivity(i);
    if (currentDifficulty.equals(preferences.getString("difficulty","problem!")))
        return true;
    else doNewGame();
    return true;

最佳答案

Android 允许您注册一个偏好更改的监听器。这是一个简单的例子:

public class myClass implements OnSharedPreferenceChangeListener
{
    private SharedPreferences settings;

    settings = PreferenceManager.getDefaultSharedPreferences(this);
    settings.registerOnSharedPreferenceChangeListener(this);

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
       // Process it here
    }
}

关于Android - 在用户访问 PreferenceActivity 后确定 Preference 是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5798308/

相关文章:

java - 从android中的webview获取可见文本

node.js - 异步/等待未在 Azure 应用服务中运行

c# - 异步任务给出警告,(没有)异步任务给出错误

android - 在不创建帐户的情况下使用 SyncAdapter

android - 为什么使用相同主题时 PreferenceActivity 和 Activity 看起来不同?

android - 对话框从带有可点击链接的首选项 Activity 开始

android - 如何获取在对话框中动态加载(setView)的布局的元素(findViewById)?

android - 与电话号码联系的 Intent

android - 在一台设备上安装应用程序的生产和测试版本

android - 某些设备 phonegap 媒体插件中的 Cordova 录音问题