旋转后恢复 CountDownTimer 对象
Restore CountDownTimer object after rotation
当我旋转时,onCreate 方法被调用,所以我丢失了所有的变量,这就是我想要恢复 CountDownTimer 对象的原因。 CDT 对象的 onTick 和 onFinish 回调方法在 countdownTimer 保持 运行 时处于活动状态。我认为解决方案是使 parcable 成为对象,但我没能完成我的代码
我不想使用服务,因为 Android 文档是特定于何时使用服务的,我认为在我的情况下我不需要服务
//my try to make parsable the CDT object
public class MyCountDownTimer implements Parcelable {
private CountDownTimer t;
public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public MyCountDownTimer createFromParcel(Parcel in) {
return new MyCountDownTimer(in);
}
public MyCountDownTimer[] newArray(int size) {
return new MyCountDownTimer[size];
}
};
private MyCountDownTimer(Parcel in) {
t = (CountDownTimer) in.readParcelable(Timer);
}
MyCountDownTimer(CountDownTimer t)
{
this.t =t;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
}
}
//declaretion
private CountDownTimer Timer;
protected void onSaveInstanceState(@NonNull Bundle outState){
super.onSaveInstanceState(outState);
outState.putSerializable(COUNTDOWN,new MyCountDownTimer(Timer));
}
//on restore
Timer = (CountDownTimer) savedInstanceState.getParcelable(COUNTDOWN);
//when call Timer
private void setTimer(long mTimeLeftInMillisfun){
Timer = new CountDownTimer(mTimeLeftInMillisfun,1000) {
@Override
public void onTick(long l) {
textTimer.setText("Remain "+ l/1000+" Seconds");
}
@Override
public void onFinish() {
//do sth
}
}.start();
}
要在配置更改时恢复您的对象,您需要将其保存在 savedInstanceState
:
override fun onSaveInstanceState(outState: Bundle) {
outState.putParcelable("your_parcelable_object", myCountDownTimer)
super.onSaveInstanceState(outState)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
super.onRestoreInstanceState(savedInstanceState)
myCountDownTimer = savedInstanceState?.getParcelable<MyCountDownTimer>("your_parcelable_object")
}
不要打包你的计时器。在 parcel/unparcel 过程中,您会浪费时间(可能不会很多,但仍然如此)。
在配置更改(activity 轮换)期间使用 ViewModel 让您的计时器存活。
https://developer.android.com/topic/libraries/architecture/viewmodel
示例代码-
public class MyViewModel extends ViewModel {
private CountDownTimer countDownTimer;
private MutableLiveData<Long> timerLiveData;
public MyViewModel() {
this.timerLiveData = new MutableLiveData<>();
}
public LiveData<Long> getTimerLiveData() {
return timerLiveData;
}
public void requestTimer(long timeInMins) {
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(timeInMins * 60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
timerLiveData.setValue(millisUntilFinished);
}
public void onFinish() {
timerLiveData.setValue(0L);
}
};
countDownTimer.start();
}
}
收听来自 UI -
的计时器
myViewModel.getTimerLiveData().observe(this, timeLeft -> Log.d("test", "timeLeft " + timeLeft);
这是 CountDownTimer.java
的定义,它没有实现 Parcelable
接口:
public abstract class CountDownTimer {
/**
* Millis since epoch when alarm should stop.
*/
private final long mMillisInFuture;
/**
* The interval in millis that the user receives callbacks
*/
private final long mCountdownInterval;
private long mStopTimeInFuture;
/**
* boolean representing if the timer was cancelled
*/
private boolean mCancelled = false;
...
并且您正在尝试使用以下代码使 un-parcelable 对象可打包:
//my try to make parsable the CDT object
public class MyCountDownTimer implements Parcelable {
private CountDownTimer t;
这是不可能的(如果有任何方法可以纠正我),除非你将 CDT 中的数据保存在 onSaveInstanceState
中,并使用你保存在 [=] 中的数据恢复它15=].
当我旋转时,onCreate 方法被调用,所以我丢失了所有的变量,这就是我想要恢复 CountDownTimer 对象的原因。 CDT 对象的 onTick 和 onFinish 回调方法在 countdownTimer 保持 运行 时处于活动状态。我认为解决方案是使 parcable 成为对象,但我没能完成我的代码
我不想使用服务,因为 Android 文档是特定于何时使用服务的,我认为在我的情况下我不需要服务
//my try to make parsable the CDT object
public class MyCountDownTimer implements Parcelable {
private CountDownTimer t;
public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public MyCountDownTimer createFromParcel(Parcel in) {
return new MyCountDownTimer(in);
}
public MyCountDownTimer[] newArray(int size) {
return new MyCountDownTimer[size];
}
};
private MyCountDownTimer(Parcel in) {
t = (CountDownTimer) in.readParcelable(Timer);
}
MyCountDownTimer(CountDownTimer t)
{
this.t =t;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
}
}
//declaretion
private CountDownTimer Timer;
protected void onSaveInstanceState(@NonNull Bundle outState){
super.onSaveInstanceState(outState);
outState.putSerializable(COUNTDOWN,new MyCountDownTimer(Timer));
}
//on restore
Timer = (CountDownTimer) savedInstanceState.getParcelable(COUNTDOWN);
//when call Timer
private void setTimer(long mTimeLeftInMillisfun){
Timer = new CountDownTimer(mTimeLeftInMillisfun,1000) {
@Override
public void onTick(long l) {
textTimer.setText("Remain "+ l/1000+" Seconds");
}
@Override
public void onFinish() {
//do sth
}
}.start();
}
要在配置更改时恢复您的对象,您需要将其保存在 savedInstanceState
:
override fun onSaveInstanceState(outState: Bundle) {
outState.putParcelable("your_parcelable_object", myCountDownTimer)
super.onSaveInstanceState(outState)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
super.onRestoreInstanceState(savedInstanceState)
myCountDownTimer = savedInstanceState?.getParcelable<MyCountDownTimer>("your_parcelable_object")
}
不要打包你的计时器。在 parcel/unparcel 过程中,您会浪费时间(可能不会很多,但仍然如此)。
在配置更改(activity 轮换)期间使用 ViewModel 让您的计时器存活。
https://developer.android.com/topic/libraries/architecture/viewmodel
示例代码-
public class MyViewModel extends ViewModel {
private CountDownTimer countDownTimer;
private MutableLiveData<Long> timerLiveData;
public MyViewModel() {
this.timerLiveData = new MutableLiveData<>();
}
public LiveData<Long> getTimerLiveData() {
return timerLiveData;
}
public void requestTimer(long timeInMins) {
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(timeInMins * 60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
timerLiveData.setValue(millisUntilFinished);
}
public void onFinish() {
timerLiveData.setValue(0L);
}
};
countDownTimer.start();
}
}
收听来自 UI -
的计时器myViewModel.getTimerLiveData().observe(this, timeLeft -> Log.d("test", "timeLeft " + timeLeft);
这是 CountDownTimer.java
的定义,它没有实现 Parcelable
接口:
public abstract class CountDownTimer {
/**
* Millis since epoch when alarm should stop.
*/
private final long mMillisInFuture;
/**
* The interval in millis that the user receives callbacks
*/
private final long mCountdownInterval;
private long mStopTimeInFuture;
/**
* boolean representing if the timer was cancelled
*/
private boolean mCancelled = false;
...
并且您正在尝试使用以下代码使 un-parcelable 对象可打包:
//my try to make parsable the CDT object
public class MyCountDownTimer implements Parcelable {
private CountDownTimer t;
这是不可能的(如果有任何方法可以纠正我),除非你将 CDT 中的数据保存在 onSaveInstanceState
中,并使用你保存在 [=] 中的数据恢复它15=].