I found the answer to this question. But I just can not figure out how to use it in my case, there is a String type, and I have a long . In the putString on the second long argument, it does not work out ...

The code itself:

 @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { outState.putString("timerValue", timer); super.onSaveInstanceState(outState); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.details_layout); chronometer = (Chronometer) findViewById(R.id.chronometer); chronometer.setBase(SystemClock.elapsedRealtime()); chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { public void onChronometerTick(Chronometer chronometer) { long t = SystemClock.elapsedRealtime() - chronometer.getBase(); chronometer.setText(DateFormat.format("mm:ss", t)); } }); chronometer.start(); } 

Edit:

 @Override protected void onSaveInstanceState(Bundle outState) { timer = SystemClock.elapsedRealtime() - chronometer.getBase(); outState.putLong("timerValue", timer); super.onSaveInstanceState(outState); } @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.details_layout); chronometer = (Chronometer) findViewById(R.id.chronometer); chronometer.setBase(SystemClock.elapsedRealtime()); if (savedInstanceState != null) { chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { public void onChronometerTick(Chronometer chronometer) { timer = savedInstanceState.getLong("timerValue"); chronometer.setText(DateFormat.format("mm:ss", timer)); } }); chronometer.start(); } else { chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { public void onChronometerTick(Chronometer chronometer) { timer = SystemClock.elapsedRealtime() - chronometer.getBase(); chronometer.setText(DateFormat.format("mm:ss", timer)); } }); chronometer.start(); } } 
  • 3
    It is possible and in the documentation to see what methods the class Bundle , and then, God forbid, you will have to put int , again all the “development” will arise. You are not going to post a question for each inappropriate method, so you will “develop” until the second coming. And in general, the development of copy-paste has not yet made anyone a programmer, it is necessary to somehow try to develop your own ability, to connect your head or something. - pavlofff
  • The fact of the matter is that I urgently need! And so, I know what to read ... Yes, and at the expense of Long, of course I was stupid, I knew that there is getLong, getString, etc., I’m just dumb ... - DevOma
  • 3
    It would take you more than 15 minutes to look at the class methods that you waited for the answer to this question ... Even the IDE itself offers all the options, it’s enough to use banal logic to solve this “problem” in a couple of seconds, but everyone has their own way, of course if especially urgently needed .. - pavlofff

1 answer 1

Let's start with what to use

 protected void onSaveInstanceState(Bundle outState) 

instead

 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) 

Bundle addition to the putString() method, the putString() has putLong() and use it.

  • For some reason, he does not want to work, please see, I updated the question ... - DevOma
  • @Omuradil, you will have to debug the code yourself. - katso
  • Did not quite understand ... - DevOma