Fixing background task bug when app is minimized and maximized quickly a...

... few times causing a dangling task to never be ended.

This makes sure to end the background task on enterForeground and also end any existing background task within enterBackground. I can usually get this problem reproduced by quickly minimizing and maximizing the app a few times and then seeing iOS force close it 180 seconds later.