You can easily exit from Roku scenegraph by using complete executions of your main method.
You need to observed field on your scene and fire a roSGNodeEvent.
Your main scene will look like this.
screen = CreateObject("roSGScreen")
m.port = CreateObject("roMessagePort")
screen.setMessagePort(m.port)
scene = screen.CreateScene("mainScene")
screen.show()
scene.setFocus(true)
while(true)
msg = wait(0, m.port)
msgType = type(msg)
if msgType = "roSGScreenEvent" then
if msg.isScreenClosed() then
return
end if
end if
end while
While loop running throughout the app and observer your event. isScreenClose() method return true when your scene would in focus but depends on your requirement and your event to close the app.
after modifying the main scene
screen = CreateObject("roSGScreen")
m.port = CreateObject("roMessagePort")
screen.setMessagePort(m.port)
scene = screen.CreateScene("mainScene")
screen.show()
scene.observeField("appExit", m.port)
scene.setFocus(true)
while(true)
msg = wait(0, m.port)
msgType = type(msg)
if msgType = "roSGScreenEvent" then
if msg.isScreenClosed() then
return
else if msgType = "roSGNodeEvent" then
field = msg.getField()
if field = "appExit" then
return
end if
end if
end if
end while
ChangeMainScene.xml so it has an observable interface boolean field called appExit
<?xml version="1.0" encoding="utf-8" ?> <!-- main node which handles home screen children --> <component name="MainScene" extends="Scene"> <interface> <!-- Specifies the content for the GridPannel --> <field id="appExit" type="bool" alwaysnotify="true" value="false"/> </interface> </component>
Setup your key event like this
function init() as Void
print "ExitApp"
end function
function onKeyEvent(key as String, press as Boolean) as Boolean
if key = "OK" then
m.top.appExit = true
end if
end function
