计时器在改变方向时重新启动



当你改变方向时,我重置了计时器值,写的是真的,但没有帮助,寻求帮助,出了什么问题

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

注意:每次用户旋转屏幕时,您的活动都会被销毁并重新创建。当屏幕改变方向时,系统会破坏并重新创建前台活动,因为屏幕配置已经更改,您的活动可能需要加载其他资源(如布局)。

请参阅:如何包含方向变化的android计时器的值

相关内容

  • 没有找到相关文章

最新更新