Method calling thread for Titanium module (Android)

When creating method for a Titanium module in Android, using @Kroll.method annotation, this method will be invoked on KrollRuntimeThread. This is important to know if that method is accessing UI component, such as TextView or WebView, since all UI related components must be handled on main thread.

@Kroll.method annotation have one convenient argument that allow method to be called on UI thread

@Kroll.method(runOnUiThread=true)
public void show() {}

However, it is deprecated – as noted on Specific Changes no. 18 of Android Module Porting Guide , you can use TiMessenger class.

Or, easier way is to use Activity runOnUiThread method

@Kroll.method
public void show() {
    getActivity().runOnUIThread(new Runnable() {
        @Override
        public void run() {
            // code here
        }
    });
}

One thought on “Method calling thread for Titanium module (Android)”

  1. I also like using this as getActivity().runOnUIThread is not always working:


    TiUIHelper.runUiDelayedIfBlock(new Runnable() {
    @Override
    public void run() {
    // code here
    }
    });

Leave a Reply

Your email address will not be published. Required fields are marked *