Detect settings change?

Good morning!

Is there a way to trigger some code of my app when settings are changed while the app is not running? I know that there is AppBase.onSettingsChanged(), but according to the documentation that one is triggered only while the app is running.

Background of the question: I'd like to clear the storage when settings are changed, since some of the entries may be obsolete. I can run some routines to check that when the app is started, but it would be more efficient to only do it when I know that settings actually have changed.

  • You could save your settings in persistent storage and check if changed at start up .

  • I'm not sure if this would work out, but you can try something with the following logic:

    1. add a boolean property, i.e: "changed" with default = false

    2. in the settings add changed=true in a way that it's always set. I'm not sure about this part, but hopefully there's something to do this. Should be similar to a hidden html input field.

    3. in the start up code check "changed" and do your logic.

    4. you also need to change back "changed" to false after your logic, as well as in onSettingsChanged

  • Thanks! I decided to just do the clean-up on every start-up.

    Basically I have a kind of pseudo array in the settings (since the real arrays are not working), and each entry has corresponding entries in the persistent storage. Originally I used on array in the storage and clean-up was taken care of automatically because when I update this one array value, it always only had the current entries. But that became a memory issue on older devices, and I had to switch to a pseudo array in storage as well, with multiple entries with an index in the name. But that means, that if the user deletes an element in the settings, its corresponding entry in the storage would stay there forever.

    Now I am just looping through all entries on startup and deleting those that are not needed. Theoretically the user could switch entries in the settings and therefore the entries in storage would not align anymore, but that risk is acceptable, it would be visible only as a temporary glitch that would set itself straight after a few seconds.

  • 2. in the settings add changed=true in a way that it's always set. I'm not sure about this part, but hopefully there's something to do this. Should be similar to a hidden html input field.

    I don't think there's a way to do this.

    I don't think you can have a setting that's always "defaulted" to a given value (regardless of the actual value of underlying property), such that the property value was overridden by the settings "default" value. I also don't think it's possible to have a hidden setting (I don't mean a property with no corresponding setting.)

    A somewhat related topic that comes up every now and then is when app developers have certain properties with no corresponding settings, and they change the default values in a later version of the app, expecting the property values for existing settings to change. But ofc they don't, bc the default values are only applied when the properties don't yet exist for the given app installation.

  • yes, but that's a feature, not a bug :) this way you can save the version in a property and later you can know in which version the app was installed (or the settings resetted) or you can update that every time you find that the current version is greater than the saved version, and perform some cleanup job

  • yes, but that's a feature, not a bug :)

    I agree with you, but I pointed it out because it's a similar kind of expectation in both cases (that somehow "new" or "forced" settings/properties will override existing properties) and it's not possible.