Back

App Events

Hello World

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());
}
    Copy     Copy All       Run      

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" );
}
    Copy     Copy All       Run      

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();
}
    Copy     Copy All       Run      

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

//Called when application is paused.
function OnPause()
{
    app.ShowPopup( "OnPause" );
}
Copy All       Run      

OnResume()

The OnPause event will be called when the user returns to your app after sending it to the background.

Example - Detect Resume

//Called when application is resumed.
function OnResume()
{
    app.ShowPopup( "OnResume" );
}
Copy All       Run      

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 );
}

//Called when screen rotates
function OnConfig()
{
    var orient = app.GetOrientation();
    txt1.SetText(orient);

    if(orient == "Portrait") orient = "Vertical";
    else orient = "Horizontal";

    lay.SetOrientation( orient );
    txt2.SetText( orient );
}
    Copy     Copy All       Run      

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 );
    // app.ToBack();
    // app.Exit();
}

//Called when alarm is triggered.
//(Even if your app is closed)
function OnAlarm( id )
{
    app.ShowPopup( "Got Alarm: id = " + id );
}
    Copy     Copy All       Run      

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

//Handle data sent from other apps.
function OnData( isStartUp )
{
    //Display intent data.
    var intent = app.GetIntent();
    if( intent )
    {
        //Extract main data.
        var s = "action: " + intent.action + "\n";
        s += "type: " + intent.type + "\n";
        s += "data: " + intent.data + "\n\n";

        //Extract extras.
        s += "extras:\n";
        for( var key in intent.extras )
            s += key+": "+intent.extras[key] + "\n";

        app.Alert( s, "OnData" );
    }
}
Copy All       Run