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:
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.
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:
B4X:
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() {
@Override
public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode e){
ba.raiseEvent(getObject(), eventName + "_failedtoreceivead", e.toString());
}
@Override
public void onReceiveAd(Ad ad) {
ba.raiseEvent(getObject(), eventName + "_receivead");
}
@Override
public void onDismissScreen(Ad arg0) {
ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_adscreendismissed", false, null);
}
@Override
public void onLeaveApplication(Ad arg0) {
//
}
@Override
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.