gpt4 book ai didi

java - 改变方向时计时器重新启动

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:13 25 4
gpt4 key购买 nike

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

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

最佳答案

警告:每次用户旋转屏幕时,您的 Activity 都会被销毁并重新创建。当屏幕改变方向时,系统会销毁并重新创建前台 Activity,因为屏幕配置已更改,并且您的 Activity 可能需要加载替代资源(例如布局)。

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

关于java - 改变方向时计时器重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37202062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com