当 Android 中的应用配置更改时,计时器会重置
Chronometer is reset on when the app configuration changes in Android
我使用 Chronometer
和 ViewModel 创建了一个秒表应用程序,该应用程序在配置更改之前运行良好。当屏幕旋转时,秒表重置为 00:00
.
this is part of StopwatchTabFrag.java
.
stopwatchTabViewModel.buttonIcon.observe(getViewLifecycleOwner(), new Observer<Integer>() {
@Override
public void onChanged(Integer imgId) {
switch (imgId){
//when the icon is changed to pause (or) when user clicks play icon then Stopwatch is start.
case R.drawable.ic_round_pause_circle_filled_24:
binding.stop.setVisibility(View.VISIBLE);
binding.startPause.setImageResource(imgId);
stopwatchTabViewModel.startStopwatch(stopwatchChronometer);
break;
//when the icon is changed to start (or) when user clicks pause icon then Stopwatch is pause.
case R.drawable.ic_round_play_arrow_24:
binding.startPause.setImageResource(imgId);
stopwatchTabViewModel.pauseStopwatch(stopwatchChronometer);
break;
case R.drawable.ic_round_stop_24:
binding.stop.setVisibility(View.GONE);
stopwatchTabViewModel.stopStopwatch(stopwatchChronometer);
}
}
});
每当更改特定按钮图标时,我都会使用 Observer 来执行此特定任务。
this is part of StopwatchTabViewModel.java
.
public void startStopwatch(Chronometer chronometer){
if (!isRunning.getValue()){
chronometer.setBase(SystemClock.elapsedRealtime()-pauseOfset.getValue());
chronometer.start();
_isRunning.setValue(true);
}
}
public void pauseStopwatch(Chronometer chronometer){
if (isRunning.getValue()){
chronometer.stop();
_pauseOfset.setValue(SystemClock.elapsedRealtime()-chronometer.getBase());
_isRunning.setValue(false);
}
}
public void stopStopwatch(Chronometer chronometer){
chronometer.setBase(SystemClock.elapsedRealtime());
_pauseOfset.setValue(0L);
chronometer.stop();
//Reset the icon to Start
_buttonIcon.setValue(R.drawable.ic_round_play_arrow_24);
}
我在 ViewModel
class 中实现了秒表任务方法。不知道对不对
期待你的回答
将此行添加到清单中的 Activity
声明中:
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
它将防止 Activity
在旋转时破坏(因此也是你的计时),你必须自己处理 onConfigurationChanged
方法调用中的旋转(你可能不需要在你的例)
HERE 你有一些关于配置更改的信息。
我使用 Chronometer
和 ViewModel 创建了一个秒表应用程序,该应用程序在配置更改之前运行良好。当屏幕旋转时,秒表重置为 00:00
.
this is part of
StopwatchTabFrag.java
.
stopwatchTabViewModel.buttonIcon.observe(getViewLifecycleOwner(), new Observer<Integer>() {
@Override
public void onChanged(Integer imgId) {
switch (imgId){
//when the icon is changed to pause (or) when user clicks play icon then Stopwatch is start.
case R.drawable.ic_round_pause_circle_filled_24:
binding.stop.setVisibility(View.VISIBLE);
binding.startPause.setImageResource(imgId);
stopwatchTabViewModel.startStopwatch(stopwatchChronometer);
break;
//when the icon is changed to start (or) when user clicks pause icon then Stopwatch is pause.
case R.drawable.ic_round_play_arrow_24:
binding.startPause.setImageResource(imgId);
stopwatchTabViewModel.pauseStopwatch(stopwatchChronometer);
break;
case R.drawable.ic_round_stop_24:
binding.stop.setVisibility(View.GONE);
stopwatchTabViewModel.stopStopwatch(stopwatchChronometer);
}
}
});
每当更改特定按钮图标时,我都会使用 Observer 来执行此特定任务。
this is part of
StopwatchTabViewModel.java
.
public void startStopwatch(Chronometer chronometer){
if (!isRunning.getValue()){
chronometer.setBase(SystemClock.elapsedRealtime()-pauseOfset.getValue());
chronometer.start();
_isRunning.setValue(true);
}
}
public void pauseStopwatch(Chronometer chronometer){
if (isRunning.getValue()){
chronometer.stop();
_pauseOfset.setValue(SystemClock.elapsedRealtime()-chronometer.getBase());
_isRunning.setValue(false);
}
}
public void stopStopwatch(Chronometer chronometer){
chronometer.setBase(SystemClock.elapsedRealtime());
_pauseOfset.setValue(0L);
chronometer.stop();
//Reset the icon to Start
_buttonIcon.setValue(R.drawable.ic_round_play_arrow_24);
}
我在 ViewModel
class 中实现了秒表任务方法。不知道对不对
期待你的回答
将此行添加到清单中的 Activity
声明中:
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
它将防止 Activity
在旋转时破坏(因此也是你的计时),你必须自己处理 onConfigurationChanged
方法调用中的旋转(你可能不需要在你的例)
HERE 你有一些关于配置更改的信息。