Java Question Tip about BA.raiseEventFromDifferentThread


B4X founder
Staff member
Licensed User
BA.raiseEventFromDifferentThread is usually used in a multithreaded library to raise an event. It works by sending a message to the main thread messages queue.

However it can also be useful in the case of a single threaded library (any library that doesn't start a new thread or uses the internal thread pool).

Regular events are only handled when the activity is active (or service).

In most cases code running in the main thread is not expected to raise events while the activity is paused. However sometimes there are special events that are expected to be raised when the activity is paused.
For example, the code of AdMob wrapper:
   public void Initialize2(final BA ba, String EventName, String PublisherId, Object Size) {
      setObject(new AdView(ba.activity, (AdSize)Size, PublisherId));
      super.Initialize(ba, EventName);
      final String eventName = EventName.toLowerCase(BA.cul);
      getObject().setAdListener(new AdListener() {

         public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode e){
            ba.raiseEvent(getObject(), eventName + "_failedtoreceivead", e.toString());
         public void onReceiveAd(Ad ad) {
            ba.raiseEvent(getObject(), eventName + "_receivead");
         public void onDismissScreen(Ad arg0) {
            ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_adscreendismissed", false, null);
         public void onLeaveApplication(Ad arg0) {
         public void onPresentScreen(Ad arg0) {
            ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_presentscreen", false, null);


As you can see AdScreenDismissed event is raised with raiseEventFromDifferentThread. This event is raised just before the ad screen is dismissed. Our activity will be paused at that time. So by using raiseEventFromDifferentThread the event will wait in the message queue and be handled just before Activity_Resume. You can also see a message in the logs saying: Running waiting messages (x) where x is the number of waiting events.


Licensed User
ba.RaiseEvent question

Hi Erel, I'm working on a ad library wrapper, so far everything works great, i'm now trying to add the events to catch when the ads load, close, or fail to load.
I have that part working on the adview part, but i would also like to implement this when an interstitial Ad loads or a video ad.

when I try to add the callback method for the interstitial ads like this
getObject().setOnInterstitialAdDownload(new UserAdDownloadAd(){

i know this gives me an error because i'm only extending the ViewWrapper<Adview> class, and not FullscreenView, therefore i can not use the
ba.raiseEvent(getObject(), eventName + "_adloaded"
code to raise this event, do you have any suggestions on how to accomplish this Erel, your help as usual will be greatly appreciated.