How to preserve state in case of long press BTN3

Posted on
  • Hi,

    in most apps, long pressing BTN3 gets you back to default UI. It performs a lot more than just unloading the current app and loading the clock app. It even executes *.boot.js files. How would you write a "background" app which actually initializes just once and provides some state for the duration of power on?

  • Short answer you don't :p

    Slightly longer answer: The classical and standard way of navigating between apps is calling the load()-function, which always clears everything, runs through boot-files starting widgets and whatnot, and then loads the chosen app (the user chosen clock when called without an argument).

    Recently Bangle.load() was introduced (fast loading) which allows to switch apps without clearing everything, saving time and crucially for a background app/widget that's supposed to 'survive' when switching apps, widgets are preserved. But to avoid ram leaks fast loading should generally be avoided for apps other than clocks and launchers, probably making a background app that is supposed to never be terminated (I don't know precisely what you have in mind) not viable.

    But.. You also have E.on('kill', function() {}) that you could use to save state of the app in a file in storage when it's killed. Then it can be used to set up the app/widget once it loads after the next call to load(). This is probably what you want (this solution is also 'power off'-proof). You can look at one example of this in BW Clocks app.js file line #437

  • Thank you for the details, I feared as much. The use case is:

    Every 30s check if the watch is in power save mode. Remember this to keep track of recent activity and act accordingly.

    Related issues:­ssues/1923­ues/2308

    For this particular use case (any activity in the last 10m), resetting the history is not a problem because BTN3 reset is activity and clearing the history does lead to the same behavior (assuming activity by default). However, this is usually an exception rather than the norm.

    Regarding E.on('kill', ...: power off proofness in this case is not desirable ;)
    Thanks again for the confirmation and detailed response!

  • @Ganblejs, I guess you mean lines #274..278 and #303...

  • Actually yes and no :P I meant what I wrote but it's outdated on the latest version of BW Clock. Here's a link to Line #437 on the version I was looking at at the time .

    So the old version shows the straight forward way to do it when not utilizing fast loading, while the new version works with fast loading.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

How to preserve state in case of long press BTN3

Posted by Avatar for !evil @!evil