Search code examples
widgettizentizen-native-app

Tizen Native: How can I have only one Widget Instance and not allow the user to create more?


I want to limit the number of Widget instances of my Widget application that the user is able to start. In fact, for my case it only makes sense to have one.

In the Samsung Gear 2 device, the music player widget has the behaviour I want. Actually when you start this widget, it is not showed again in the Widget list. This would be perfect!

The Schedule Widgets also have this behaviour. There are project samples for the music player and for the Schedule widgets but in the samples this behaviour does not happen.

Any suggestions?


Solution

  • I found here the solution: https://developer.tizen.org/zh-hans/forums/native-application-development/limit-number-widget?langswitch=zh-hans

    We can do it programatically by adding a variable to count the widget's instances

    int instance_count =0;
    

    Then in the widget instance create callback we check the variable and increment it if there is only one instance or return error if it already has one instance created

    static int
    _on_create_cb(widget_context_h context, bundle *content, int w, int h, void *user_data) {
    
        if(instance_count >0) return WIDGET_ERROR_ALREADY_EXIST;
        instance_count++;
    
        return WIDGET_ERROR_NONE;
    }
    

    In the destroy callback we decrement the counter

    static int
    _on_destroy_cb(widget_context_h context, widget_app_destroy_type_e reason, bundle *content, void *user_data) {
        instance_count--;
        return WIDGET_ERROR_NONE;
    }
    

    This is not the perfect solution because it still allows the user to select the widget from the list, but it works. I also tested crashing the app on purpose and this solution works. When the app crashes the _on_destroy_cb is not called, but the instance_count is reset to 0 anyway.