I have a simple app that counts steps using Sensor.TYPE_STEP_DETECTOR. This sensor is NOT unregistered after the app is set into the background, meaning it continues to track steps even when it's not in focus. Once the app resumes, the step counter UI gets updated with whatever steps were counted since it was last in focus.
My goal is to have the app save the data after it's been closed in the background. It already saves it onPause() but I haven't found any method related to when the app is closed.
At first, onDestroy() seemed like the obvious place, but it's not guaranteed to be called since it only seems to be called when something calls finish() or if the system wants to clear up space.
I've also seen solutions involving using a background service or Job Scheduler. A background service wouldn't work since as of API >= 26 the background services stop after several minutes. I think Job Scheduler wouldn't work since I won't know when the user might close the app and I want the steps to be as accurate as possible, so finding an interval for when to schedule the jobs would be difficult.
I'm looking for a simple spot to basically do what my onPause() method already does.
@Override
protected void onPause() {
super.onPause();
saveSteps(currentSteps);
running = false;
}