在PreferenceActivity中更改首选项时,在Android主活动中进行响应

时间:2013-11-08 17:49:55

标签: android android-activity sharedpreferences preferenceactivity onresume

我想从PreferenceActivity返回时更新主要活动视图。我已经阅读了许多有关此问题的帖子并实施了不同的方法,但没有办法......

方法1 :使用startActivityForResult()

public boolean onOptionsItemSelected( MenuItem item ) {
    switch( item.getItemId() ) {
        case R.id.about:
            AboutDialog about = new AboutDialog(this);
            about.setTitle(getString(R.string.About));
            about.show();
            break;
        case R.id.menu_settings:
            startActivityForResult(new Intent(MainActivity.this, SettingsActivity.class),-1);
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    this.charts.redraw();
    super.onActivityResult(requestCode, resultCode, data);
}
永远不会调用 onActivityResult()。我也尝试使用有效的requestCode但结果是一样的。

方法2 :使用OnSharedPreferenceChangeListener

public class MainActivity extends Activity implements OnItemSelectedListener, View.OnClickListener, OnCheckedChangeListener, OnSharedPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        this.charts.redraw();
    }

但是再次onSharedPreferenceChanged()永远不会被调用。我认为这不是因为众所周知的垃圾收集器问题,因为我的监听器是MainActivity对象而不是临时对象。

方法3 :使用onResume()

public class MainActivity extends Activity implements OnItemSelectedListener, View.OnClickListener, OnCheckedChangeListener, OnSharedPreferenceChangeListener {

    @Override
    protected void onResume() {
        this.charts.redraw();
        super.onResume();
    };

但是onResume()并没有像预期的那样被调用,实际上它是在PreferenceActivity刚刚启动时调用的!!! ??

最后,PreferenceActivity:

@SuppressWarnings("deprecation")
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

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

    private void initSummaries( PreferenceGroup pg ) {
        for( int i = 0; i < pg.getPreferenceCount(); i++ ) {
            Preference p = pg.getPreference(i);
            if( p instanceof PreferenceGroup )
                initSummaries((PreferenceGroup)p);
            else
                setSummary(p);
        }
    }

    private void setSummary(Preference pref) {
        if( pref instanceof ListPreference ) {
            ListPreference listPref = (ListPreference)pref;
            pref.setSummary(listPref.getEntry());
        }
    }

    public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
        Preference pref = findPreference(key);
        setSummary(pref);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }
}

如果我重新启动应用程序,则会显示新值,因此它们会正确存储在共享首选项中。

我错过了什么吗?这非常令人沮丧...

1 个答案:

答案 0 :(得分:1)

您似乎没有在setResult上致电PreferencesActivity