Fixing background task bug when app is minimized and maximized quickly a few times causing a dangling task to never be ended