Event Dispatching fails when Called from Initialize

package space.themelon.dispatchtest;

import android.util.Log;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.Form;

@DesignerComponent(version = 1,
        category = ComponentCategory.EXTENSION,
        nonVisible = true)
@SimpleObject(external = true)
public class DispatchTest extends AndroidNonvisibleComponent {

  private static final String TAG = "DispatchTest";

  public DispatchTest(Form form) {
    super(form);
  }

  @SimpleFunction
  public void DispatchEvent() {
    MyEvent("hello world");
  }

  @SimpleEvent
  public void MyEvent(String arg) {
    boolean dispatched = EventDispatcher.dispatchEvent(this, "MyEvent", arg);
    Log.d(TAG, "Event Dispatched = " + dispatched);
  }
}

The above code contains simple blocks, a block called DispatchEvent() that calls MyEvent("hello world"). This is expected to work correctly, but the following behaviour happens:

  • When DispatchEvent() block is called from Initialize event block, EventDispatcher.dispatchEvent() returns false.

  • When DispatchEvent() block is called on an event of a Button click, the event gets dispatched successfully.

[kumaraswamy@ekita ~]$ adb logcat -s DispatchTest
--------- beginning of main
05-19 22:12:29.488  5765  5765 D DispatchTest: Event Dispatched = false
05-19 22:12:31.039  5765  5765 D DispatchTest: Event Dispatched = true

DispatchTest.aia (5.7 KB)
space.themelon.dispatchtest.aix (5.0 KB)

Tested in compiled application.

2 Likes
package space.themelon.dispatchtest;

import android.os.Handler;
import android.util.Log;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.Form;

@DesignerComponent(
        version = 1,
        description = "A test extension to dispatch an event.",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "images/extension.png"
)
@SimpleObject(external = true)
public class DispatchTest extends AndroidNonvisibleComponent {

    private static final String TAG = "DispatchTest";

    public DispatchTest(Form form) {
        super(form);
    }

    @SimpleFunction(description = "Dispatches MyEvent with the given argument.")
    public void DispatchEvent() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                MyEvent("hello world");
            }
        }, 100); 
    }

    @SimpleEvent(description = "This event is triggered with a string argument.")
    public void MyEvent(String arg) {
        boolean dispatched = EventDispatcher.dispatchEvent(this, "MyEvent", arg);
        Log.d(TAG, "Event Dispatched = " + dispatched);
    }
}

Hopefully, this Java code can do what you want to do @Kumaraswamy

1 Like

This is by design. No events can be dispatched until the screen finishes initializing and its Initialize event runs. If you have events you need to dispatch, you should defer them until after initialization of the screen has completed.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.