如果用户按下R.id.home或后退按钮,如何放弃PreferenceActivity中的更改?

时间:2017-02-10 11:31:58

标签: android

我希望在PreferenceActivity中所做的任何更改都不会被保存,除非用户按下"完成"我已添加到ActionBar的按钮,因此,如果按下后退按钮,更改将不会应用,并且开关将更改回以前的值。

enter image description here

更新

根据@Ravindra Kushwaha的建议,我详细阐述了以下代码:

boolean push_switch, push_on, push_off, push_stats;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupActionBar();
    push_switch = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("push_switch", true);
    push_on = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("push_on", true);
    push_off = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("push_off", true);
    push_stats = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("push_stats", true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch(id)
    {
        case android.R.id.home:
            onBackPressed();
            return true;
        case 1000: // Done button
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed() {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("push_switch", push_switch);
    editor.putBoolean("push_on", push_on);
    editor.putBoolean("push_off", push_off);
    editor.putBoolean("push_stats", push_stats);
    editor.apply();
    finish();
}

似乎按预期工作并符合我的期望,但是......

这是正确的方法,还是不建议在任何方面?我发现它很奇怪......让android自己应用更改,如果按下则返回手动还原它们,再次应用前一个...

我认为,如果我按下,只要按下 ,那就更容易了。

2 个答案:

答案 0 :(得分:0)

首先,你必须得到你onCreate中的切换的值,即toggle的先前状态,它是关闭还是打开,如下所示: -

boolean push_switch, push_on, push_off, push_stats;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupActionBar();
    push_switch = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("push_switch", true);
    push_on = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("push_on", true);
    push_off = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("push_off", true);
    push_stats = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("push_stats", true);

 ////And than after , you have to set these values in yours toggle like:
   toggleFirst.setChecked(push_on)
   toggleSecond.setChecked(push_off)
   toggleThird.setChecked(push_switch)
   toggleForth.setChecked(push_stats)

}

  /////Perfom click listeners in yours all toogle like below and save the value in the boolean variables
YOUR_TOGGLE_BUTTONS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked)
    {
        push_on = true;   ///// i am assign all value together , you have to maintain setOnCheckedChangeListener for each toggle and assign the vale..for the example ,i have done these
        push_off= true;
        push_switch= true;
        push_stats= true;

    }
    else
    {
        push_on = false;
        push_off= false;
        push_switch= false;
        push_stats= false;

    }

}
});
  //////And in the last on Done button click listener save your data like below

 YOUR_DONE_BUTTON.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            /////HERE SAVE THE VALUE INSIDE THE SHARED PREFRENCE..
        }
    });

答案 1 :(得分:-1)

为“完成”按钮取一个标记。 输入首选项活动时,将所有优先级值保存为默认值。 如果用户点击“完成”,则相应地更改它们,否则将值更改为首选项。