In your app you can use various event functions which will be automatically called by DroidScript.
OnStart()
This is probably the most popular one. It will be called when DroidScript has initialized and it is the last function which will be called from global scope. That means every timeout you might have set will be called after OnStart was called.
When OnStart has returned, the apps 'started' state will be set to true. Therefore app.IsStarted() method will return true as well.
Example - OnStart
setTimeout(OnLoad);
function OnStart()
{
alert("called OnStart\nApp Started: " + app.IsStarted());
}
function OnLoad()
{
alert("called OnLoad\nApp Started: " + app.IsStarted());
}
OnMenu(name)
This event is called when the user selects an item from the in-app menu.
See Also: SetMenu, ShowMenu
Example - OnMenu
function OnStart()
{
app.SetMenu( "Start,Stop,Pause" );
lay = app.CreateLayout( "linear", "" );
btn = app.CreateButton( "[fa-gear]", -1, -1, "fontawesome" );
btn.SetOnTouch( app.ShowMenu );
lay.AddChild( btn );
app.AddLayout( lay );
}
function OnMenu( item )
{
app.ShowPopup( item, "Short" );
}
OnBack()
By default the app closes if the user presses the devices back-button. However, you can disable that behaviour by calling app.EnableBackKey(false).
In this case the OnBack event will be called instead of closing the app. This can be useful to create a confirmation dialog before exiting:
Example - Confirm Exit
function OnStart()
{
app.EnableBackKey( false );
yndExit = app.CreateYesNoDialog("Exit App?");
yndExit.SetOnTouch( yndExit_OnTouch );
app.ShowPopup( "Press the back button" );
}
function yndExit_OnTouch(reply)
{
if(reply == "Yes") app.Exit();
}
function OnBack()
{
yndExit.Show();
}
OnPause()
The OnPause event will be called when the user sends the app to the background, ie. when pressing the home button.
Example - Detect Pause
function OnPause()
{
app.ShowPopup( "OnPause" );
}
OnResume()
The OnResume event will be called when the user returns to your app after sending it to the background.
Example - Detect Resume
function OnResume()
{
app.ShowPopup( "OnResume" );
}
OnConfig()
OnConfig is called when a device configuration changes, especially the screen orientation. This can be used to rearrange your layouts on orientation change.
Example - Layout Orientation
function OnStart()
{
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
txt1 = app.CreateText( "" );
txt1.SetTextSize( 64 );
lay.AddChild( txt1 );
txt2 = app.CreateText( "" );
txt2.SetTextSize( 64 );
lay.AddChild( txt2 );
OnConfig();
app.AddLayout( lay );
}
function OnConfig()
{
var orient = app.GetOrientation();
txt1.SetText(orient);
if(orient == "Portrait") orient = "Vertical";
else orient = "Horizontal";
lay.SetOrientation( orient );
txt2.SetText( orient );
}
OnAlarm()
If you have set up an app Alarm and it is triggered it will call the OnAlarm event.
Example - OnAlarm
function OnStart()
{
var now = Date.now();
app.SetAlarm( "Set", 1234, OnAlarm, Date.now() + 3000 );
}
function OnAlarm( id )
{
app.ShowPopup( "Got Alarm: id = " + id );
}
OnData()
When an other app has sent an intent to your app you will get notified by the OnData event. Then you can retrieve the intent object using the app.GetIntent() method.
Example - Received Intent Data
function OnData( isStartUp )
{
var intent = app.GetIntent();
if( intent )
{
var s = "action: " + intent.action + "\n";
s += "type: " + intent.type + "\n";
s += "data: " + intent.data + "\n\n";
s += "extras:\n";
for( var key in intent.extras )
s += key+": "+intent.extras[key] + "\n";
app.Alert( s, "OnData" );
}
}
OnDrawer(side, state)
When the user opens or closes a drawer the OnDrawer callback is called.
state can be “Open” or “Closed”
side can be “Left” or “Right”