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.

https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

de.bodymindpower.ActivityLifecycle.aix (6.1 KB)

grafik

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): https://community.kodular.io/t/taifun-tools-activity-changed/69554/12?u=bodymindpower

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

1 Like

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

1 Like

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.