如何同时使用onSaveInstanceState(Bundle outState)和onSaveInstanceState(Bundle savedInstanceState)?

时间:2018-03-05 10:12:41

标签: onsaveinstancestate

我试图通过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");
         }

1 个答案:

答案 0 :(得分: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);
}

只要更改参数中的名称,都是一样的事情。