0

I have a countdown timer that takes a long as parameter and then it shows a countdown of that time. When i click the backbutton and exit the app, I want the remaining time ofthe countdown timer to be saved in a sharepreference in onStop() method. And when i reopen the app I want to take the remaining time as the parameter and start the timer again, until it hits 0.

I have troubles because if I my parameter is 2, then countdown timer counts down from 1 minutes and 59 secs. And when I exit the app and the reopen the app it still shows 1 minutes and 59secs. The countdown timer doesnt take the subtract of what time is left. It just shows the same time as entered.

private void startTimer() {
    countDownTimer = new CountDownTimer(timee*60000 , 1000) {
        @Override
        public void onTick(long l) {

            tv.setText(simpleDateFormat.format(l));

            timesLeft = l;

        }

        @Override
        public void onFinish() {
            tv.setText("00:00:00");}
}.start();

Here is onStop() and onStart();

protected void onStart() {
    super.onStart();
    SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);


    if(1>0){

    long s = sPrefs.getLong("sysstoptime", 0);
    long tt = sPrefs.getLong("timeleft", 0);
    long currenttime = System.currentTimeMillis();
    long k = sPrefs.getLong("timeset", 0);


    s = s+tt;

    timee = (s-currenttime)/60000+1;


    startTimer();}

    else {
        Toast t5 = Toast.makeText(this, "less than or equal 0", Toast.LENGTH_LONG);
        t5.show();
    }

}

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


    SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
    SharedPreferences.Editor editor = sPrefs.edit();

    editor.putLong("sysstoptime", System.currentTimeMillis());
    editor.putLong("timeleft", timesLeft);
    editor.putLong("timeset", timeset);




    editor.apply();


}

What im trying to do is that when the countdown timer is running and I am leaving the app, i am taking the System.currenttimeinmillis + the remaining time of countdown timer. That is variable s+tt. Then I equal timee to the remaining time of s+tt - System.currenttimeinmillis, to have countdown timer, countdown the remaining time left including the time when app was closed/stopped. Then I call the method startTimer(); with my new parameter of timee. But it doesn't show the remaining time, instead it shows the same time as i enter when i exit and enter app.

timee is long variable, and the user choses what timee is, its in minutes. so if timee = 10, then it means 10 minutes and countdown timer will count from 09:59

jonny
  • 313
  • 3
  • 12
  • There are a couple issues. First, do not try to perform your saving of data in onStop(), use onPause() instead. Secondly, do not try to save the countdown timer value, timestamp the event instead. When you return back into the app, take the current time and then compare to the timestamped time to get the difference. It will be easier. – portfoliobuilder Apr 16 '18 at 23:54
  • why is it wrong to save data in onStop()? – jonny Apr 17 '18 at 00:07
  • could you please show me how to solve my problem. – jonny Apr 17 '18 at 00:09

1 Answers1

0

First, when you get value to countdowning, multiply it by 60000 after press start button [or whatever you use to call startTimer()].

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       timee = YOUR_VALUE_TO_COUNTDOWN * 60000;
       startTimer();
    }
});

change startTimer() like that:

    private void startTimer() {
    countDownTimer = new CountDownTimer(timee, 1000) {
        @Override
        public void onTick(long l) {
            tv.setText(simpleDateFormat.format(l));
            timesLeft = l;
        }

        @Override
        public void onFinish() {
            tv.setText("00:00:00");
        }
    }.start();
}

change onStop()

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

    SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
    SharedPreferences.Editor editor = sPrefs.edit();
    editor.putLong("timeleft", timesLeft);
    editor.putLong("sysstoptime", System.currentTimeMillis());
    editor.apply();
}

and finally onStart()

@Override
protected void onStart() {
    super.onStart();
    SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);

    if(1>0){            
        timesLeft = sPrefs.getLong("timeleft", 0);
        long stopTime = sPrefs.getLong("sysstoptime", 0);
        long currentTime = System.currentTimeMillis();
        timee = timesLeft - (currentTime - stopTime);
        startTimer();
    }
    else {
        Toast t5 = Toast.makeText(this, "less than or equal 0", Toast.LENGTH_LONG);
        t5.show();
    }
}
Szamot
  • 366
  • 2
  • 12
  • thanks this works too, but what i tried to do, was to run the countdowntimer in the background when the app was closed. So when i returned to the app, the countdown timer had subtracted the time i was away from the app, and gives me a real time that is left on countdown timer. – jonny Apr 17 '18 at 02:52
  • Ok, I edited my code, that it worked like counting in the background – Szamot Apr 17 '18 at 15:38
  • thanks, but it should be timee = stopTime+timesLeft - currentTime. Because stopTime + timesLeft will always be bigger than currentTime, unless countdown Timer has already finished the countdown, which will cause the method to not run. – jonny Apr 17 '18 at 18:29
  • also one user said save data in onPause instead of onStop, dont know why, but i will check it out. – jonny Apr 17 '18 at 18:31
  • timee = stopTime+timesLeft - currentTime; timee = timesLeft - (currentTime - stopTime); both are doing exactly the same, it's just order of operations. Here you could read about doubts where to save data https://stackoverflow.com/questions/29480890/when-to-save-data-to-database-onpause-or-onstop Or you could reach for the source in official Android Guide about activity-lifecycle: https://developer.android.com/guide/components/activities/activity-lifecycle.html – Szamot Apr 17 '18 at 21:00