0

I'm running a countdowntimer in a fragment and everytime I press the back button onDestroy() is called and kills my countdowns. When I launch my app again a new activity has started and the countdowns are gone.

onDestroy() isFinished() returns true.

This wont happend if I press the home button.

How can I keep this from happening? Use services? Set flags? override the back button? I'm lost here.

aelveborn
  • 357
  • 1
  • 4
  • 17

4 Answers4

0

You are breaking the normal Android lifecycle with this approach, if the user presses back is because he wants to leave your app no matter what.
But anyway, you can avoid the behavior overriding onBackPressed and make the Back key acts like the Home key. Just have a look here Override back button to act like home button

Community
  • 1
  • 1
Antonio
  • 3,128
  • 2
  • 22
  • 14
  • I guess this would be my last option since this still leaves the option for the garbagecollector to destroy my activity, when it feels like it. – aelveborn Aug 09 '12 at 13:39
0

When the Back Button is pressed the activity/process is destroyed. So yes to get around this use a Service, if its vital that this timer keep running running then use a Foreground Service (requires a constant Notification). A Foreground Service can not be destroyed, where normal Service can be if system memory is low and needs to be reclaimed. In my experiences this does not happen very often (especially on new devices).

Jug6ernaut
  • 8,219
  • 2
  • 26
  • 26
  • When you press the back button, that doesn't necessarily destroys the activity, just puts it in the background, and the grabage collector may, or may not destroy it in the near future. Also a foreground service can be destroyed as well, it is just not likely, and it will be recreated as soon, as there is sufficient memory available. – Adam Monos Aug 09 '12 at 13:22
0

Your timer is running in the context of the Activity, so when the activity is destroyed so is the timer. When home is pressed the activity is put into a paused state, hence the different actions.

Move to a background service which can then be started when required and will continue running in the background when the activity is destroyed. Be aware that even though you have a background service it may still get killed by the device if the memory is needed so you will need to handle that as well.

Sync the countdown timer with the device time. That way if the service is destroyed you can then re-sync the countdown time using the devices current time.

Also take a look at the AlarmManager, which will be useful in this situation - http://developer.android.com/reference/android/app/AlarmManager.html

Phil H
  • 897
  • 4
  • 10
  • Background services seems like a good idea, but I really like combining AlarmManager (for notifying the user when the timer is done) and storing my objects containing all the system times. Though it seems like both AlarmManager and background service both require a WakeLock. – aelveborn Aug 09 '12 at 13:43
  • The wake lock is not a device wake lock, it ensures that when the service is handling the alarm the device does not shut it down. So, you need to listen for the alarm and then when the alarm is triggered your app will start doing it's thing. You shouldn't need to get a Display Wake Lock to use them. – Phil H Aug 09 '12 at 13:52
0

The destruction of your activity is out of your control. It fully depends on the garbage collector. If it needs to clean up memory, and your activity is in the background, it will destroy it most likely.

You should use a service with startForeground(...) to make sure it will only be destroyed when it is absoslutely necessary. Also don't forget to acquire a WakeLock in your service, if you want to keep it alive while the screen is locked.

Adam Monos
  • 4,287
  • 1
  • 23
  • 25
  • Is WakeLock vital for a foreground service to work? I'am not dependent on every tick since Im storing the starttime in an class. But I am dependent on the last tick since that will trigger my alarm. – aelveborn Aug 09 '12 at 13:32
  • If you don't have a `WakeLock` then your service won't do anything until you release the screen lock, so you won't be able to handle the last tick when it occurs. A `PARTIAL_WAKE_LOCK` is what you need. It won't keep the screen alive, but it will allow your service to continue running properly. – Adam Monos Aug 09 '12 at 13:35
  • Also, you don't have to worry about battery usage, since a simple countdown won't make any noticable difference in power consumption. – Adam Monos Aug 09 '12 at 13:36
  • Thank you @Adam L. Mónos, battery consumption was my worries. But a `PARTIAL_WAKE_LOCK` seems like the way to go. I need to figure out where to invoke this foreground service since my `MainActivity`creates an `ActionBar` with tabs. The `TabListener` adds the `fragments` (wich is my timers). But this may be an whole new question. – aelveborn Aug 09 '12 at 13:51