2

i need a countDownTimer keep running when i swap between activities.. i have more than one activity, i put the countDownTimer in the main activity but when i swap to another activity and back to the main activity it turns back to count again from the start, i believe because the method countDownTimer is onCreate method.

So, how should I go about doing this?

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

    questionTime();
}

public void updateTimer(int secondsLeft){
    int minutes = (int) secondsLeft / 60;
    int seconds = secondsLeft - minutes * 60;
    String secondString = Integer.toString(seconds);

    timerTextView.setText(Integer.toString(minutes) + ":" + secondString);
}

private void questionTime(){
    new CountDownTimer(10000, 1000){

        @Override
        public void onTick(long millisUntilFinished) {
            updateTimer((int) millisUntilFinished / 1000);

        }

        @Override
        public void onFinish() {
            timerTextView.setText("0:00");
            Log.i("finished", "timer Done");
        }
    }.start();
}

Update: That helped me to reach my purpose How to run CountDownTimer in a Service in Android?

  • You could check the `savedInstanceState` in `onCreate()` and only start the timer if it is null. See also [the documentation on instance state](https://developer.android.com/guide/components/activities/activity-lifecycle#instance-state) – Bö macht Blau Jul 10 '18 at 19:21

2 Answers2

0

Maybe this is a little far fetched, but the way that I think to solve this issue and not worrying for the Activities is using an IntentService.

Even if you store some sort of value in the Bundle of the onSaveInstance() hook method this can lead to some pretty messy results if you enable the "Don't keep activities" flag in the device's settings.

What I would do is create an IntentService that when It's triggered starts the countdown, then It broadcast the changes of that countdown through EventBus/Otto/BroadcastReceiver back to the UI.

Another way of doing it is having the countdown instance in your Application class, and check it from there.

I would go with the IntentService solution because having a countdown instance running in the Application class sounds a little off.

Let me know if you want any specifics on how to implement the IntentService but a little bit of Googling should show you how to do it.

4gus71n
  • 3,717
  • 3
  • 39
  • 66
  • 1
    Thank you, you declare to me the idea That helped me to implement it in android studio https://stackoverflow.com/questions/22496863/how-to-run-countdowntimer-in-a-service-in-android – Abdelrhman Ahmed Jul 11 '18 at 15:10
0

As soon as the time starts, write the time (unix timestamp) to properties file. And when the user comes back to your main activity, read the properties file and compare it the time in the properties file with current timestamp and update the timer based on that.

N0000B
  • 409
  • 1
  • 7
  • 16