anonymous function in event handler

220 views
Skip to first unread message

salvatore fusto

unread,
Jun 27, 2014, 2:41:11 AM6/27/14
to androi...@googlegroups.com
Hi' all,
i'm trying to use an anonymous function in a button event handler:
btn.SetOnTouch(function({myCallback(arg1,arg2...)})) to pass arguments to the callback, but on running i always obtain an invalid token error on the very first line of the code, even if this is an instruction, a comment or a blank line.
can anyone help me to solve?
thanks and regards
salvatore


Dave Smart

unread,
Jun 27, 2014, 4:38:39 AM6/27/14
to androi...@googlegroups.com
Hi Salvatore,

Sorry, AndroidScript does not support anonymous functions as callback parameters.  This is due to a limitation within the Android WebView->Java bridge.

But this might be a good thing in the long run as too many Anonymous functions can make code very messy and hard to read (especially for beginners)

Regards
David

salvatore fusto

unread,
Jun 27, 2014, 11:22:55 AM6/27/14
to androi...@googlegroups.com
thanks Dave, but how can i call a callback with arguments?
regards
Salvatore

Dave Smart

unread,
Jun 27, 2014, 12:25:27 PM6/27/14
to androi...@googlegroups.com
Hi Salvatore,

You can't pass your own parameters to AndroidScript callbacks as AndroidScript will often pass it's own set of parameters for various callbacks.  Although the SetOnTouch callback of the button control does not actually send any parameters (but the Image control does). 

You can however use the app.GetLastButton() method to get the last button pressed.  Also, since AndroidScript objects are all JavaScript objects you can add custom properties and methods, so you might like to save your parameter values within the button object and retrieve them in a shared OnTouch handler like this:-


function OnStart()
{
   lay = app.CreateLayout( "linear", "VCenter,FillXY" );

   btn1 = app.CreateButton( "Press Me", 0.3, 0.1 );
   btn1.SetOnTouch( btn_OnTouch );
   lay.AddChild( btn1 );
   btn1.myVal = "hi from button 1";
    
  btn2 = app.CreateButton( "Press Me", 0.3, 0.1 );
  btn2.SetOnTouch( btn_OnTouch );
  lay.AddChild( btn2 );
  btn2.myVal = "hi from button 2";
  app.AddLayout( lay );
}

function btn_OnTouch()
{
  app.ShowPopup( app.GetLastButton().myVal );
}


Does this help?

salvatore fusto

unread,
Jun 29, 2014, 6:35:30 AM6/29/14
to androi...@googlegroups.com
Dave this may be a solution but imho it is not a best practic as anomimous funcs are much more abstract.
Thanks and regardds
Salvatore

Mark van der Velden

unread,
Feb 22, 2016, 5:35:32 AM2/22/16
to DroidScript
Hi, I tried something similar:
 
xxxx.Button = function(parent) {
    this.parent = parent;
    this.handle = app.CreateButton(...);
 
    this.OnTouch = function() {
        "dosomething";
    }
 
    this.handle.SetOnTouch(this.OnTouch);
    this.parent.AddChild(this.handle);
}
 
This gives the same error as an anonymous function. Is this the same problem?
 
I have not found a way to have the callback live in the namespace xxx. With the info above I can work around it ;-).
 
Mark

Steve Garman

unread,
Feb 22, 2016, 5:43:17 AM2/22/16
to DroidScript
It is the same problem but the problem has often been described narrowly as being about anonymous functions.

In fact, DroidScript controls cannot take anything but a global function as a callback.

Reply all
Reply to author
Forward
0 new messages