Search code examples
onsaveinstancestate

How can I use both onSaveInstanceState(Bundle outState) and onSaveInstanceState(Bundle savedInstanceState)?


I am trying to save displayed time of a chronometer by onSaveInstanceState(Bundle savedInstanceState). However, there is already an onSaveInstanceState(Bundle outState) in my code, so I see the warning of "onSaveInstanceState(Bundle) is already defined in the scope".

How can I use both Bundle savedInstanceState and Bundle outstate?

Here is the onSaveInstanceState method(s):

  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);
     }

Here is the onCreate method:

 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");
         }

Solution

  • they are actually the same thing! the name is just a dummy variable which you can easily change with no problem. You code could be:

    protected void onSaveInstanceState(Bundle outState) {
             super.onSaveInstanceState(outState);
             outState.putInt("CLICK_NUMBER", clickNumber);
             ...
             answer43.setTextColor(0xAA76FF03);
    
             // new code here
             outState.putString("myVar", myVar);
    }
    

    or you can use

    protected void onSaveInstanceState(Bundle savedInstanceState) {
             super.onSaveInstanceState(outState);
             savedInstanceState.putInt("CLICK_NUMBER", clickNumber);
             ...
             answer43.setTextColor(0xAA76FF03);
    
             // new code here
             savedInstanceState.putString("myVar", myVar);
    }
    

    It will be the same thing as long as you change the name in the argument.