我制作了一个完美运行的 CountDownTimer - 您可以获得正确的时间来软/中/硬煮鸡蛋。我的问题是定时器在方向改变后重置。我已经用谷歌搜索并尝试了很多解决方案,但我仍然不明白如何正确使用 onSave 和 onRestore。这是我的代码:
有小费吗?
package com.dohman.boilaneggbae;
import android.graphics.PorterDuff;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private static final String CURRENT_TIME = "currentTime";
private static final String DURATION_TIME = "durationTime";
private long currentTime;
private int durationTime;
private TextView time;
private Button buttonLargeSize;
private Button buttonMediumSize;
private Button buttonSoft;
private Button buttonMedium;
private Button buttonHard;
private Button buttonHellaHard;
private CountDownTimer countDownTimer;
private EggSize mediumOrLarge = EggSize.UNDEFINED;
private boolean alreadyRunning = false;
enum EggSize {
UNDEFINED, MEDIUM, LARGE;
}
private View.OnClickListener btnMediumSizeClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
mediumOrLarge = EggSize.MEDIUM;
}
};
private View.OnClickListener btnLargeSizeClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
mediumOrLarge = EggSize.LARGE;
}
};
private View.OnClickListener btnSoftClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
if ((mediumOrLarge != EggSize.UNDEFINED) && (alreadyRunning == false)) {
alreadyRunning = true;
durationTime = 240;
start(240);
} else if (mediumOrLarge == EggSize.UNDEFINED) {
time.setText("Choose size first");
} else {
alreadyRunning = false;
cancel();
}
}
};
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putLong(CURRENT_TIME, currentTime);
savedInstanceState.putInt(DURATION_TIME, durationTime);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
currentTime = savedInstanceState.getLong(CURRENT_TIME);
durationTime = savedInstanceState.getInt(DURATION_TIME);
}
time = findViewById(R.id.time);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currentTime = savedInstanceState.getLong(CURRENT_TIME);
durationTime = savedInstanceState.getInt(DURATION_TIME);
currentTime -= durationTime;
}
private void start(int duration) {
time.setText("");
if (mediumOrLarge == EggSize.MEDIUM) {
duration -= 60;
}
countDownTimer = new CountDownTimer(duration * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
String text = String.format(Locale.getDefault(), "%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) % 60,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) % 60);
time.setText(text);
}
countDownTimer.start();
}
}