Forum Discussion

360tv's avatar
360tv
Streaming Star
2 years ago
Solved

Trying to use back button to exit program.

A channel I'm working on needs to be able to back out to the main Roku home screen when I press the back button on my main screen using a ZoomRowList, but it's not happening. Here's what I have so far.

 

This is my main.brs

 

sub Main()
	screen = CreateObject("roSGScreen")
	m.port = CreateObject("roMessagePort")
	screen.setMessagePort(m.port)
	scene = screen.CreateScene("HomeScene")
	scene.observeField("exitApp", m.port)

	m.global = screen.getGlobalNode()
	m.global.id = "GlobalNode"
	m.global.addFields({ feed: get("https://xxxx.com/feed") })
	screen.show()

	while(true)
		msg = wait(0, m.port)
		? msg
		? "Msg Type = " + type(msg)
		msgType = type(msg)

		if msgType = "roSGScreenEvent" then
			if msg.isScreenClosed() then
				return
			else if msgType = "roSGNodeEvent" then
				field = msg.getField()
				? "field = " + field
				if field = "exitApp" then
					? "exitApp"
					return
				end if
			end if
		end if
	end while
end sub

 

 

This is the key event handler in HomeScreen.brs

 

function onKeyEvent(key as string, press as boolean) as boolean
	? "ZoomRowList ButtonPress " + press.tostr() + key
	if press = true then
		if key = "options"
			? m.zoomRowList
		end if
	else
		if key = "OK"
			? "Selected Item " + m.currentItem.id
			m.screenFadeOut.control = "start"
			m.top.setFocus(false)
			'm.videoInfoScreen.setFocus(true)
			m.videoInfoScreen.callFunc("ShowScreen", m.currentItem)
		else if key = "back"
			? "end"
			? m.top.exitApp
			m.top.exitApp = true
			? m.top.exitApp
		else if key = "INFO"
			' Add conditions for other buttons as needed
		end if
	end if
	return true
end function

 

Homescreen.xml

 

<?xml version="1.0" encoding="utf-8"?>

<component name="HomeScene" extends="Scene">

	<script type="text/brightscript" uri="pkg:/components/HomeScene/HomeScene.brs" />
	<script type="text/brightscript" uri="pkg:/components/tools.brs" />
	<interface>
		<field id="exitApp" type="bool" value="false"/>
	</interface>
	<children>

	</children>

</component>

 

 When I press back, the output from the keypress handler in homescreen.brs works and prints out what I'm looking for, but the main.brs does not seem to respond at all or exit the app. Any thoughts on what I'm doing wrong?

  • `scene.setFocus(true)` was missing from main.brs.

5 Replies

  • In main.brs you've nested your check for roSGNodeEvent inside your check for roSGScreenEvent, so you'll never hit it.

    		if msgType = "roSGScreenEvent" then
    			if msg.isScreenClosed() then
    				return
    			else if msgType = "roSGNodeEvent" then
    				field = msg.getField()
    				? "field = " + field
    				if field = "exitApp" then
    					? "exitApp"
    					return
    				end if
    			end if
    		end if

     

    It looks like it should work, apart from that - though I'd maybe check 'msg.getData() = true' as well just to protect me from my future idiot self.

    • 360tv's avatar
      360tv
      Streaming Star
      	while(true)
      		? m.port
      		msg = wait(0, m.port)
      		? msg
      		? "Msg Type = " + type(msg)
      		msgType = type(msg)
      
      		if msgType = "roSGScreenEvent" then
      			if msg.isScreenClosed() then
      				return
      			end if
      		else if msgType = "roSGNodeEvent" then
      			field = msg.getField()
      			? "field = " + field
      			if field = "exitApp" then
      				? "exitApp"
      				return
      			end if
      		end if
      	end while
       
      `? msg` should print whatever's there, reguardless, but it never prints anything. exitapp is defined as shown in my firstpost. 
      • 360tv's avatar
        360tv
        Streaming Star
        `scene.setFocus(true)` was missing from main.brs.