Android 旋转后还原倒计时对象

Android 旋转后还原倒计时对象,android,bundle,parcelable,countdowntimer,onsaveinstancestate,Android,Bundle,Parcelable,Countdowntimer,Onsaveinstancestate,当我旋转时,onCreate方法被调用,所以我丢失了所有变量,这就是为什么我要恢复CountDownTimer对象。当倒计时持续运行时,CDT对象的onTick和onFinish回调方法处于活动状态。我认为解决方案是使对象可包裹,但我没有设法完成我的代码 我不想使用服务,因为Android doc在使用服务时是特殊的,我想在我的情况下,我不需要服务 //我尝试使CDT对象可解析 公共类MyCountDownTimer实现了可包裹{ 专用倒计时器t; public final Parcelable

当我旋转时,onCreate方法被调用,所以我丢失了所有变量,这就是为什么我要恢复CountDownTimer对象。当倒计时持续运行时,CDT对象的onTick和onFinish回调方法处于活动状态。我认为解决方案是使对象可包裹,但我没有设法完成我的代码

我不想使用服务,因为Android doc在使用服务时是特殊的,我想在我的情况下,我不需要服务

//我尝试使CDT对象可解析
公共类MyCountDownTimer实现了可包裹{
专用倒计时器t;
public final Parcelable.Creator=新Parcelable.Creator(){
公共MyCountDownTimer createFromParcel(地块中){
返回新的停机计时器(in);
}
公共MyCountDownTimer[]新数组(整数大小){
返回新的MyCountDownTimer[大小];
}
};
专用停机计时器(包裹输入){
t=(倒计时)in.readParcelable(计时器);
}
MyCountDownTimer(倒计时t)
{
t=t;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效书面包裹(包裹,内部i){
}
}
//宣布
私人倒计时;
SaveInstanceState上受保护的无效(@NonNull Bundle outState){
super.onSaveInstanceState(超出状态);
outState.putSerializable(倒计时,新的MyCountDownTimer(计时器));
}
//恢复时
计时器=(倒计时)savedInstanceState.getParcelable(倒计时);
//何时调用计时器
私有void setTimer(长MTIMELFTINMILLISFUN){
计时器=新的倒计时计时器(mtimelftinmillisfun,1000){
@凌驾
公共空白条(长l){
text timer.setText(“保持”+l/1000+“秒”);
}
@凌驾
公共无效onFinish(){
//做某事
}
}.start();
}

若要在配置更改时恢复对象,需要将其保存在
保存状态状态中

 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")
}
覆盖SaveInstanceState(超出状态:捆绑){
outState.putParcelable(“您的可包裹对象”,myCountDownTimer)
super.onSaveInstanceState(超出状态)
}
覆盖RestoreInstanceState(savedInstanceState:Bundle?){
super.onRestoreInstanceState(savedInstanceState)
myCountDownTimer=savedInstanceState?.getParcelable(“您的\u parcelable\u对象”)
}

若要在配置更改时恢复对象,需要将其保存在
保存状态状态中

 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")
}
覆盖SaveInstanceState(超出状态:捆绑){
outState.putParcelable(“您的可包裹对象”,myCountDownTimer)
super.onSaveInstanceState(超出状态)
}
覆盖RestoreInstanceState(savedInstanceState:Bundle?){
super.onRestoreInstanceState(savedInstanceState)
myCountDownTimer=savedInstanceState?.getParcelable(“您的\u parcelable\u对象”)
}

不要打包你的计时器。在包裹/解包裹过程中,您将在时钟上损失时间(可能不是很多,但仍然如此)

在配置更改(活动轮换)期间,使用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();
}

不要打包你的计时器。在包裹/解包裹过程中,您将在时钟上损失时间(可能不是很多,但仍然如此)

在配置更改(活动轮换)期间,使用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();
}

这是
CountDownTimer.java
的定义,它没有实现
Parcelable
接口:

公共抽象类倒计时{
/**
*从报警停止的时间算起毫秒。
*/
私人最终长远计划;
/**
*用户接收回调的间隔(毫秒)
*/
私人最终长时间间隔;
私人长期信息;
/**
*表示计时器是否已取消的布尔值
*/
私有布尔值mCancelled=false;
...
您正在尝试使用以下代码将不可包裹的对象设为可包裹:

//我尝试使CDT对象可解析
公共类MyCountDownTimer实现了可包裹{
专用倒计时器t;

这是不可能的(如果有任何方法,请纠正我),除非您将CDT中的数据保存在
onSaveInstanceState
中,并使用您保存在
onCreate
中的数据将其还原。这是
倒计时程序的定义。java
,它不实现
Parcelable
接口:

公共抽象类倒计时{
/**
*从报警停止的时间算起毫秒。
*/
私人最终长远计划;
/**
*用户接收回调的间隔(毫秒)
*/
私人最终长时间间隔;
私人长期信息;
/**
*表示计时器是否已取消的布尔值
*/
私有布尔值mCancelled=false;
...
您正在尝试使用以下代码将不可包裹的对象设为可包裹:

//我尝试使CDT对象可解析
公共类MyCountDownTimer实现了可包裹{
专用倒计时器t;

这是不可能的(如果有任何方法,请纠正我),除非您将CDT中的数据保存在
onSaveInstanceState
中,并使用您在
onCreate
中保存的数据将其还原。谢谢您的回答,但我的问题是将parcelable设置为countdownTimer对象。谢谢您的回答,但我的问题是将parcelable设置为countdownTimer对象。是否要将
保存为Co解除停机计时器