Not getting observeField (or observeFieldScoped) to firing callback
Trying to mash together two (or three) examples I found, to create a dialog that will fire if you press back arrow when on the home screen (or technically the last screen in the stack array).
So what I am trying to achieve is a logic along the lines of:
1. Intercept the backarrow key (onkeyevent)
2. Display dialog for "Confirm Exit"
3 observeField "buttonSeleted" and set callback to "onButtonSelected"
4. Return true to tell Roku that we handled the backarrow
Then later (buttonSelected)
5. set m.top.exitApp = true (which breaks the while loop and exits the program)
I'm fairly certain step 1-4 is working, but step 5 is never called. (the print to the debug log never happens)
Here is the code:
sub showExitDialog()
m.exitDialog = createObject("roSGNode", "Dialog")
m.exitDialog.backgroundUri = "pkg:/images/dialog-background.png"
m.exitDialog.title = "Close channel"
m.exitDialog.buttons = ["Exit", "Cancel"]
m.exitDialog.optionsDialog = true
m.exitDialog.message = "Do you want to close Channel"
m.exitDialog.observeField("buttonSeleted", "onButtonSelected")
m.top.dialog = m.exitDialog
end sub
'detect selection
sub onButtonSelected()
'use the exitDialoge Button Selected field
print "Serring Exit to true"
if m.exitDialog.buttonSelected = 0 then
print "Exiting app"
m.top.exitApp = true
else
print "Doing nothing"
end if
end subAnd the code to intercept the back arrowfunction onKeyEvent (key as String, press as boolean) as boolean
handled=false
if press then
if (key = "back") then
handled=true
print "You pressed back"
showExitDialog()
end if
end if
return handled
end function
Also, I stole from two different examples, where the first one is
https://developer.roku.com/en-gb/docs/references/scenegraph/standard-dialog-framework-nodes/standard-dialog-framework-overview.md
And the second one was how to break the while loop
https://rymawby.com/brightscript/roku/Exiting-out-of-a-brightscript-scenegraph-app.html
Also, the example used observeFieldScoped, but could not find any documentation on what that does. (A bit new to brigthscript, so sorry if that is obvious)
If the code you've shown is a copy and paste then you have a typo:
m.exitDialog.observeField("buttonSeleted", "onButtonSelected")Should be buttonSelected.
But that's not the only problem. Even if you fix the line, add a print in front of it and I bet it prints out false meaning the statement failed and your observer will never be called.
print m.exitDialog.observeField("buttonSelected", "onButtonSelected")That "buttonSelected" needs to be attached to a component. I'm not sure how to accomplish that; I've only ever done it via an interface alias in the XML file for the node/group/component or whatever it's called.
XML:
<interface>
<field id="button_selected"
type="assocarray"
alias="dialog.buttonSelected"
/>
</interface>
<children>
<Dialog
id="dialog"
/>
</children>
</component>BRS:
print m.top.observeField("button_selected", "onButtonSelected")I tried a quick test trying to add it to the code you have, but I couldn't come up with the right way to specify it or I had some other problem with the code since it's not complete.
As for observeField vs observeFieldScoped, I've wondered the same thing, but I've never taken the time to investigate it.
I have to include my standard caveat: As I've stated many times, I'm not the person to ask about scene graph as I absolutely hate it. Maybe if I
wastedtook the time to learn more than the bare minimum I wouldn't hate it so much, but I don't see that ever happening.