Here is an extension to get the activity lifecycle state of an app. So e.g. whether an app is switched in the background and / or in idle (sleep) mode.
Note:
According to my experience and tests, however, onStopis always triggered after onPause.
The following have been tested: alarm, incoming phone call, idle mode, split screens, ... Maybe someone will find a scenario where this doesn't happen.
Or maybe @ewpatton can explain, when this might happen.
Yes, it should always be the case that onPause is called before onStop. The logic is in Android's ActivityThread class, concretely that performStopActivityInner calls performPauseActivityIfNeeded, so if the activity is not in the paused state it will be paused before stopped.
No, it does not imply that. All it implies is that you can't go to a stopped state without first going through the paused state. Going from onPause to onResume is a valid path and happens often. To see it, start the companion app (no need to connect) and do the following:
adb logcat | egrep -i "Form.*(pause|resume)"
And then turn the screen on and off a few times. You should see messages like this:
06-29 12:51:22.630 19439 19439 I Form : Form Screen1 got onResume
06-29 12:51:53.009 19439 19439 I Form : Form Screen1 got onPause
06-29 12:51:56.106 19439 19439 I Form : Form Screen1 got onResume
06-29 12:51:57.594 19439 19439 I Form : Form Screen1 got onPause
06-29 12:52:03.564 32353 32353 I Form : Form Screen1 got onResume
06-29 12:52:13.012 32353 32353 I Form : Form Screen1 got onPause
06-29 12:52:17.690 32353 32353 I Form : Form Screen1 got onResume
onPause is called whenever the app goes into the background, such as when the screen is locked. When the screen is unlocked, onResume is called because the app will come into the foreground.
I could not execute this (adb logcat) command. But even if this were the case, there must be an error in my or the Tools extension from Taifun or the AppOnPause /Stop /Resume events of Kodular, because onStop is also always triggered there.
(I'll send you the java code of the extension via PM.)
which means, @Anke is correct according to my tests?
@ewpatton you might want to test again using for example adb logcat | egrep -i "Form.*(pause|resume|stop)"
@Anke, please don't forget to follow the naming conventions... UpperCamelCase for property, method and event names (i.e. the first letter should be a capital letter) and lowerCamelCase for parameter names
Yes, that's true. On a phone it does appear to call onStop for an activity because the first activity is completely replaced by the new activity. If you use an app in Split Screen mode on a tablet or on a Chromebook, focusing on the second app will result in an onPause event but no onStop because the first app is still on the screen, just not active.
I have a timer that always counts up, which should also count in the background. I added the extension for that. But the problem is that it doesn't always work, so sometimes it works great, sometimes it doesn't. If the screen is on but I'm not in the app, then it works too. It just doesn't work when the screen is closed.
First, thanks for the quick response.
I'm making a time tracking app that counts and saves the working hours - a so-called time clock. So the timer should always run when the screen is on but I'm out of the app when the screen is closed etc. So the timer should always run.
Back Pressed:
Back pressed is for not being able to push back.
EingestempeltTxt:
Stamped Text is a label that shows when one has Stamped.
EingestempeltBtn:
Stamped Button is a TimePicker where you can optionally enter when you stamped.
Clock2:
There are three texts in Clock2. One for seconds, one for minutes and one for hours. If seconds is greater than 59, than seconds is reset to 0 and text minutes is plus 1. If minutes is greater than 59, than minutes is reset to 0 and hours of text are plus 1. And with TinyDB, the working hours are saved so that you can later see how long you worked, etc.
Initialize:
When initializing, Clock2 is set to True so that it should count up as soon as it starts, like other time clocks.