1

我正在尝试通过 onSaveInstanceState(Bundle savedInstanceState) 保存计时器的显示时间。但是,我的代码中已经有一个 onSaveInstanceState(Bundle outState),所以我看到“onSaveInstanceState(Bundle) 已经在范围内定义”的警告。

如何同时使用 Bundle savedInstanceState 和 Bundle outstate?

这是 onSaveInstanceState 方法:

  protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putInt("CLICK_NUMBER", clickNumber);
         outState.putBoolean(BUTTON2CORRECT_CHECKED_KEY, answer2.isChecked());
         outState.putBoolean(BUTTON21_CHECKED_KEY, wrongAnswer21.isChecked());
         outState.putBoolean(BUTTON22_CHECKED_KEY, wrongAnswer22.isChecked());
         outState.putBoolean(BUTTON23_CHECKED_KEY, wrongAnswer23.isChecked());
         outState.putBoolean(BUTTON3CORRECT_CHECKED_KEY, answer3.isChecked());
         outState.putBoolean(BUTTON31_CHECKED_KEY, wrongAnswer31.isChecked());
         outState.putBoolean(BUTTON32_CHECKED_KEY, wrongAnswer32.isChecked());
         outState.putBoolean(BUTTON33_CHECKED_KEY, wrongAnswer33.isChecked());
         outState.putBoolean(CHECKBOX41_CHECKED_KEY, answer41.isChecked());
         outState.putBoolean(CHECKBOX42_CHECKED_KEY, answer42.isChecked());
         outState.putBoolean(CHECKBOX43_CHECKED_KEY, answer43.isChecked());
         outState.putBoolean(CHECKBOX44_CHECKED_KEY, answer44.isChecked());
         outState.putBoolean(CHECKBOX45_CHECKED_KEY, answer45.isChecked());
         outState.putBoolean(BUTTON5CORRECT_CHECKED_KEY, answer5.isChecked());
         outState.putBoolean(BUTTON51_CHECKED_KEY, wrongAnswer51.isChecked());
         outState.putBoolean(BUTTON_CLICKED_KEY, buttonIsClicked);
         answer2.setTextColor(0xAA76FF03);
         answer3.setTextColor(0xAA76FF03);
         answer5.setTextColor(0xAA76FF03);
         answer41.setTextColor(0xAA76FF03);
         answer42.setTextColor(0xAA76FF03);
         answer43.setTextColor(0xAA76FF03);

     }

     @Override

     protected void onSaveInstanceState(Bundle savedInstanceState) {

         super.onSaveInstanceState(savedInstanceState);
         savedInstanceState.putString("myVar", myVar);
     }

这是 onCreate 方法:

 protected void onCreate(Bundle savedInstanceState) { // TODO
 Auto-generated method stub
         super.onCreate(savedInstanceState);
         setContentView(R.layout.html_new_page);
         chronometer = findViewById(R.id.chronometer);
         chronometer.start();
         myVar = chronometer.getText().toString();

         if (savedInstanceState == null) {

             myVar = chronometer.getText().toString();
         } else {
             myVar = savedInstanceState.getString("myVar");
         }
4

1 回答 1

0

它们实际上是同一回事!该名称只是一个虚拟变量,您可以轻松更改它而没有问题。您的代码可能是:

protected void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putInt("CLICK_NUMBER", clickNumber);
         ...
         answer43.setTextColor(0xAA76FF03);

         // new code here
         outState.putString("myVar", myVar);
}

或者你可以使用

protected void onSaveInstanceState(Bundle savedInstanceState) {
         super.onSaveInstanceState(outState);
         savedInstanceState.putInt("CLICK_NUMBER", clickNumber);
         ...
         answer43.setTextColor(0xAA76FF03);

         // new code here
         savedInstanceState.putString("myVar", myVar);
}

只要您更改参数中的名称,这将是同一件事。

于 2018-08-14T20:33:43.797 回答