CEF BrowserComponent - 'onload' event called twice on Android devices

151 views
Skip to first unread message

P5music

unread,
Feb 3, 2021, 10:26:30 AM2/3/21
to CodenameOne Discussions
I have development experience on Android,
and I know that the WebView has some quirkness, for example it has two methods for catching the "onload" event. In one Android app I had to manage that with a workaround.

My Android build of the CodenameApp seems to have that issue too.

Indeed on the simulator the onload callback is called once
while
on the Android device it is called twice.
 
I realized that because I put logging instructions, and also I saw some functions being called twice.

Are you aware that the callbacks of "onload" event could be called twice on the Android devices?
I think it is important.

Can you fix that or I have to create a workaround?

And do you know about this issue on iOS devices?

Thanks in advance

Shai Almog

unread,
Feb 3, 2021, 9:50:06 PM2/3/21
to CodenameOne Discussions
The iOS API is generally better/more consistent than the Android API and doesn't have as many quirks. If you can provide a reference to the recommended Android workaround we can incorporate it.

P5music

unread,
Feb 4, 2021, 4:54:50 AM2/4/21
to CodenameOne Discussions
On Android that issue can be very nasty also on the separate Chrome WebView (or maybe it was, I do not know if now it is fixed), so the only workaround is taking into account what really happens.
It is mainly because the two methods are not always called in the same order.
And nothing prevents the WebView from firing again for no reason shortly after, so it can happens more than twice. I think this is/was a bug.

I can say what I am doing in my CodenameApp:
I have a class boolean variable, like onLoadEventAlreadyCalled. One different variable has to be used for each BrowserComponent.
I have two BrowserComponents in two different classes.

The trick is to set the boolean to false 
each time
a method is called that can originate a "onLoad" event (case sensitive or use BrowserComponent.onLoad const),

then in the listener (addWebEventListener) have:

if (onLoadEventAlreadyCalled) return;
onLoadEventAlreadyCalled=true;

It is important to set the boolean to false before calling any methods that can originate the event because otherwise the code will be executed only the first time.

So it depends on your implementation. I experienced two events, so what method are you using? It could be that the same method is being called twice, as I said.

Regards

Shai Almog

unread,
Feb 5, 2021, 1:01:59 AM2/5/21
to CodenameOne Discussions
AFAIK we don't have a workaround for this bug. Feel free to file an issue on that with a suggestion for that workaround.
Reply all
Reply to author
Forward
0 new messages