Lifecycle OnResume and OnPause strange behavior

226 views
Skip to first unread message

Pascal Georges

unread,
May 26, 2023, 1:35:58 AM5/26/23
to DroidScript
Hello,

I have sometimes (rarely) an issue when resuming an app where the state is not correctly restored. 

Given that the namings are the same, I expected a similar behavior. But for example when OnStart() is called from DS, OnResume() is not which is not consistent with Android activity lifecycle where OnResume is always called after an OnStart() event.

So do you know any resource that could explain DS behavior in deeper details ?

The bug I sometimes get is the following : when an app is resumed, its state is not the latest one, but the previous one, as if the latest OnPause event did not manage to save the state, hence the resume is made with the former saved state.

Pascal Georges

Steve Garman

unread,
May 26, 2023, 2:43:35 AM5/26/23
to DroidScript
Do you have any code in the OnPause event?

Pascal Georges

unread,
May 26, 2023, 3:25:52 AM5/26/23
to DroidScript
Yes, sure : 

    function OnPause() {
        if (state.isRunning)
            clearInterval(clockID);
        saveState();
        if (!DEBUG)
        app.SetVolume("Music", volumeDeviceSaved);
    }

Where saveState is defined as :

function saveState() {
    state.clock_target = document.getElementById("clock_target").innerHTML;
    app.SaveJson( "piano_state", state, "piano_state.json" );
}

So there are 2 calls to app.* functions (SetVolume and SaveJson), could it be possible OnPause event could not complete and the state left with inconsistent data ?

Pascal Georges

Right2TheV0id

unread,
May 27, 2023, 9:20:41 AM5/27/23
to DroidScript
This is a possibility.
You should test with a significant number of long actions to see if (and when) the OnPause function is interrupted.
If this is the case, maybe using a service to save the state of your app would fix your issue.

For the documentation (other than the wiki) there is this page on the built-in doc:

Pascal Georges

unread,
May 27, 2023, 11:06:37 AM5/27/23
to DroidScript
Hi, 

So the event handler is not atomic due to the call of app functions, or is it always the case ? 
For my application using a service is more complicated than saving the application state evevry time it changes. 

Pascal Georges 

Dave

unread,
May 28, 2023, 7:34:45 AM5/28/23
to DroidScript
You could try using the 'NoPause' option if you are using a webview or app.SetOptions( "NoPause" ) if using an HTML/Hybrid app.  Webviews/Browser windows (including the javascript) are normally paused by Android shortly after loosing focus.

However the best way would probably to make sure that you do as little as possible in the OnPause event.  Just keep a global variable which records/mirrors the current state rather than trying to execute javascript on the DOM and then reset the state you want when the OnResume callback is called (when you have more time to do complex tasks).

Pascal Georges

unread,
May 28, 2023, 12:01:59 PM5/28/23
to DroidScript
Thanks Dave for your support.

For app.SetOptions( "NoPause" ), there is no such NoPause option in the doc https://symdstools.github.io/Docs/docs/app/SetOptions.htm  where only “UseBrowser”, “NoKeys” or “ShowKeys”, “NoThemeAlerts”, “IgnoreSslErrors”, “TouchModal”, “NoTouch”, “NoFocus”, “Secure”, “ShowLocked”, “WakeScreen”, “ScreenOn” are listed.
Anyway I tried this option but did not notice any difference in the event cycle : OnPause, OnResume, etc. are called in the same way.

I will use your hint on limiting the code and DOM access in OnPause function, thanks for that.

Pascal Georges

Reply all
Reply to author
Forward
0 new messages