There is a countdown timer for 10 seconds, after the expiration of time a new activity opens, but when the device rotates, the timer is reset, trying to save the seconds value to int and use onSaveInstanceState (), but nothing happened (And I need the application to be able to turn) , Help me to understand! Here is the code:

public void startTimer() { cTimer = new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { mTimer.setText("" + millisUntilFinished / 1000); } public void onFinish() { mTimer.setText("done!"); Intent intent = new Intent(MainActivity.this, MainActivity2.class); /*Здесь реализация*/ intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }; cTimer.start(); } public void cancelTimer() { if(cTimer!=null) cTimer.cancel(); } 
  • you, apparently, somehow not correctly saved in onSaveInstanceState. Show how saved, we will fix. - Vladyslav Matviienko
  • when you rotate the device, the Activity is recreated, and therefore the values ​​are reset. For the temporary storage of values ​​in such cases, onSaveInstanceState is really used, so most likely the reason is its implementation ... - ZigZag

2 answers 2

Most likely, you only saved the data in the onSaveInstanceState method, but did not restore it in the onRestoreInstanceState method:

 protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); //тут вы должны восстанавливать данные, сохранённые в методе onSaveInstanceState } 

    In Manifest.xml in the active, set the parameter android:configChanges="keyboardHidden|orientation|screenSize"

    • This also saves the ArrayList that was randomly generated - 2Ra66it