Best practice loading bitmaps

Hi,

Just wondering if someone could let me know what the best practice is regarding the loading of resources (in particular, bitmaps) into a datafield.

1. Load them into a local variable in onUpdate(dc) when they are required to be displayed
2. Load them into an instance attribute in initialize() so the loading is done only once
3. Load them into an instance attribute in onShow() and then assign null to those attributes in onHide()
4. Some other way that I haven't mentioned.

Thanks
  • Ideally you would load them in onShow() and unload them in onHide(). This way they are unloaded when you switch to a new view, freeing up memory for that view to use.

    Travis
  • Hi Travis,

    Thanks for that, now when you say to unload them, do you mean simply assigning null to the variable? And also, are you saying to implement onShow and onHide even in a datafield? Like this:

    class df extends Ui.DataField
    {
    hidden var bmp;

    function onShow() {
    bmp = Ui.loadResource(Rez.Drawables.bmp);
    }

    function onHide() {
    bmp = null;
    }

    function initialize() { ... }
    function onLayout(dc) { ... }
    function compute(info) { ... }
    function onUpdate(dc) { ... }

    }


    So is that what you mean? Thanks for the help.
  • Yes, this is exactly what I was suggesting.

    If you are implementing a data field or a watch face, the onHide() function should never be called (according to the documentation for Ui.View), so the memory won't be released until the app exits. Since you can't push views with those application types, it should not matter.

    Travis
  • Former Member
    Former Member over 8 years ago
    I think the onHide function will get called for Data Fields when they leave the active view. There isn't anything else for your app to load in that case though (another view that belongs to that app is not coming in), so there isn't much reason to unload resources in that app type.

    With the addition of Goal Views, there are situations where your main view will get closed (and your Goal View opened) on a Watchface. The main view will get destroyed, so any resources loaded to it, should get cleared as a part of that process, but if you loaded resources in something that is held by the AppBase, you may need to release it when this transition happens.