1

I reset the chronometer value when you change the orientation, has written is true, but does not help, ask for help, what went wrong

@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();
}
}
No Name
  • 741
  • 8
  • 18

1 Answers1

0

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

See: How to contain value of android chronometer with change in orientation

Community
  • 1
  • 1
jmunoz
  • 91
  • 1
  • 3
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Enamul Hassan Aug 05 '16 at 03:54