Extension - Activity Lifecycle of an app (onPause → onStop → onResume)

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.

Update Nov. 2, 2020:
de.bodymindpower.ActivityLifecycle.aix (6.2 KB)
now I followed the naming conventions, thanks for the hint @Taifun.

grafik

Update - April 26, 2023:
Added: Move app to background
ActivityLifecycle_3.aix (5.2 KB)

grafik

Note:
According to my experience and tests, however, onStop is 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.

See also here (video): Taifun Tools - Activity Changed - #12 by bodymindpower - Discuss - Kodular Community

17 Likes

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.

4 Likes

Yes, but does that imply that there is no way from onPause back to onResume (without triggering onStop)?

According to Google, it should be possible to:

grafik

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.)

To check this: activityLifecircle.aia (51.1 KB)

Blocks

Screenshot

grafik

I tried this and get the following result
Unbenannt
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

Taifun

2 Likes

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.

@Anke Please add onDestroy and onRestart
Thank you :star_struck:

From what I found, these two callbacks never get invoked for extensions.
@pavi2410 Kindly elaborate it a little more.

4 Likes

sir dont have onDestroy????????????????????????

Don't spam with question marks please, they don't look nice.

1 Like

sir we want to execute a command before user close the app not on app is not visible. so that the onDestroy will be our need. thanks

it badly need lang sir.. sorry...

Well I am sure about that.
You can't get it.

2 Likes

so you mean there's no solution sir.

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.

Show your blocks.
And what do you mean by "background"? Is the app closed or just not visible but not closed?

2 Likes

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.

I would like to create an app like this:

The Blocks:

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.

The question is, is it even possible to create such an app here? If not, it is possible with other similar programs such as Kodular etc.