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?

Do you have the exitApp field defined

https://github.com/rymawby/roku-app-exit/blob/master/components/MainScene.xml

https://github.com/rymawby/roku-app-exit

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.

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.

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

Interesting, OK!

I’m glad you’ve got it working, but scene.setfocus is not something I’ve had to do… You may have some other focus related issue which shows itself in a different way in the near future, so keep an eye out.