Java Question raiseEvent and subExists

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I've seen a couple different ways of calling events in libraries.

1. Check to see if sub exists with subExists then if so raiseEvent.
2. Just call raiseEvent.

Now, since I haven't seen a problem either way I'm assuming the first thing raiseEvent does is to call subExists? So, depending on your school of though as to whether cleaner code or less overhead for method calls are better we should be fine just calling raiseEvent right?

I ask because I'm making a view with only one event and to make proper use of the view you have to use the event. Since the event should always exist then calling subExists only to have raiseEvent call it a 2nd time would be overkill.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Nothing happens if you call raiseEvent and the sub doesn't exist. Still subExists is useful as you do not want to create a listener at all if the sub doesn't exist:
B4X:
if (ba.subExists(eventName + "_click")) {

         getObject().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
               ba.raiseEvent(ViewWrapper.this.getObject(), eventName + "_click");
            }

         });
      }

Note that for UI events it is better to call raiseEventFromUI.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Ok, that sounds good since it could override events in libraries that inherit from wrappers and such it would make sense not to even change the event if no sub.

So, on raiseEventFromUI...I've not seen that in existing library code posted to the forums yet. Is that something new for 2.50+? I still reference the old core and shared libraries so 2.50 isn't a requirement in my library...although I thought it was just one function I never use that changed in 2.50. What does it do different than the regular raiseEvent? I'm guessing by UI you would mean pretty much any event as most of the events passed are click, longclick, IME stuff, etc. Was all the existing view/wrapper code changed to use it then?
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Ok, I remember that now, back when people were posting about the Recycled Twice stuff. So, it looks like instead of pausing execution it just posts/queues the event to the message loop. What happens in events like- Touch, EditorAction, and the Key Events that need a return value? Does it just return False? I may have to just keep the old way for those.

I seem to remember having an issue with my Back Key processing when that version came out with it no longer stopping the event from bubbling up the chain, or the default handling of Back not closing or something...some of my code still has an Activity.Finish even though I think it was fixed in the next B4A version.

So, how does this effect things like my prompt to exit the app when Back is pressed?
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
hello Everybody,

Anyone know where is the problem with this RaiseEvent?


PIECE OF LIBRARY CODE
public class mqtt implements MqttCallback {

@Hide
private BA objba;
@Hide
private String eventName;

public void Initialize(BA ba,String eventname)
{
objba = ba;
Log.d("B4A", "JAVA [Initialize] eventname = " + eventname);

if (ba.subExists(eventname + "_oninitialized"))
{
Log.d("B4A", "JAVA [Initialize] subExists = True");
objba.raiseEvent(objba, eventname + "_oninitialized");
}
else
{
Log.d("B4A", "JAVA [Initialize] subExists = False");
}
}


LOG (SHOWING THE EVENT EXIST IN B4A)
** Activity (main) Create, isFirst = true **
JAVA [Initialize] eventname = objmqtt
JAVA [Initialize] subExists = True

EVENT IN B4A (not fired by raiseevent)
Sub objmqtt_oninitialized()
Msgbox("[objMqtt_oninitialized]","MQTT")
End Sub




What I do wrong???? somebody help me!

thanks in advance

Alberto
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…