恢复活动后无法检索countDownTimer剩余的timeMillis值

时间:2018-10-05 11:36:41

标签: android countdowntimer

当活动开始并且 milliRemaining 变量被全局声明时,我启动了countDownTimer

@Override
protected void onStart() {
    super.onStart();

    countDownTimer = new CountDownTimer(120000 , 1000){

        @Override
        public void onTick(long millisUntilFinished) {

            millisRemaining = millisUntilFinished;

            seconds = (long) (millisUntilFinished / 1000);
            minutes = seconds / 60;
            seconds = seconds % 60;
            mCountdownTimerTextView.setText(String.format("%02d", minutes)
                    + ":" + String.format("%02d", seconds));

        }

        @Override
        public void onFinish() {
            mCountdownTimerTextView.setText("Done");
        }
    }.start();

}

在saveInstance方法中存储millisRemaining的值

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putLong("millisLeft" , millisRemaining);

}

和在restoreInstancestate方法中,我通过 millisRemaining

调用 onTick 方法
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    millisRemaining = savedInstanceState.getLong("millisleft");

    System.out.println(millisRemaining/1000);

    countDownTimer.onTick(millisRemaining);
}

它正确显示时间,直到活动没有暂停。我是android新手,请纠正我。另外,我是否以正确的方式实施此操作。

更新

这是活动的代码

public class MainActivity extends AppCompatActivity {

private long seconds , minutes , millisRemaining;
CountDownTimer countDownTimer = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
    super.onStart();

    countDownTimer = new CountDownTimer(120000 , 1000){

        @Override
        public void onTick(long millisUntilFinished) {

            millisRemaining = millisUntilFinished;

            seconds = (long) (millisUntilFinished / 1000);
            minutes = seconds / 60;
            seconds = seconds % 60;
            mCountdownTimerTextView.setText(String.format("%02d", minutes)
                    + ":" + String.format("%02d", seconds));

        }

        @Override
        public void onFinish() {
            mCountdownTimerTextView.setText("Done");
        }
    }.start();

    @Override
protected void onSaveInstanceState(Bundle outState) {

    outState.putLong("millisLeft" , millisRemaining);
    super.onSaveInstanceState(outState);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    millisRemaining = savedInstanceState.getLong("millisleft");

    System.out.println(millisRemaining/1000);

    countDownTimer.onTick(millisRemaining);
}

}

1 个答案:

答案 0 :(得分:1)

只需将代码从Removing them will correct the form validation放入class Login extends CI_Controller { public function index() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->form_validation->set_rules('user_email', 'Username', 'required'); $this->form_validation->set_rules('user_pass', 'Password', 'required'); if ($this->form_validation->run() == FALSE) { $this->load->view('login'); } else { echo "Form Submit."; } } } 。为了更好地理解,请参见App Lifecycle

编辑:

<?php echo form_open(); ?>
    <?php echo validation_errors('<div class="error">', '</div>'); ?>
    <input type="text" name="user_email" value="<?php echo set_value('user_email'); ?>">
    <input type="password"" name="user_pass" value="<?php echo set_value('user_pass'); ?>">
    <input type="submit" value="Login">
<?php echo form_close(); ?>