Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Back Button Thoughts
Started by laurasweet8888 Nov 04 2017 01:24 PM

- - - - -
6 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

laurasweet8888

[GLOBAL: userInfoPane.html]
laurasweet8888
  • Contributor

  • 111 posts
  • Corona SDK

Poking around the forum I ran across this:  

--  main.lua

-- require the composer library
local composer = require "composer"
composer.gotoScene( "scene1" )

-- Key listener
local platformName = system.getInfo( "platformName" )

local function onKeyEvent( event )
    if ( platformName == "Android" ) then
        if event.phase == "down" then
            if event.keyName == "back" then
                local currScene = composer.getSceneName( "current" )
                local prevScene = composer.getSceneName( "previous" )
                if currScene == "firstScene" then
                    -- present an alert asking if the user wants to quit the app.
                elseif prevScene then
                    composer.gotoScene( prevScene )
                end
                return true
            end
        end
    end
end

Runtime:addEventListener( "key", onKeyEvent )

When I started playing with it I noticed a couple things.

 

1.  system.getInfo( "platformName" )  has been deprecated.   Use "platform" instead. 

 

2.  On my simulator no matter what I do, this command always returns win32.  So I can't test  this code.  

Do we really need the platform check?   Apple isn't going to send us these keys anyway right?  

 

3.  On my android phone,  the back key isn't processed unless it is released.  So this code should check for "up" instead of "down".  

 

4.  It would be slightly more efficient to check for "up" after the check for "back".  This code is processing any keyboard key that is pushed down, not just the back button.  

 

Maybe this:  

local function onKeyEvent( event )
	if ( event.keyName == "back" ) then
		if event.phase == "up" then   -- released back key 
			exitpage() 
		end 
		return true   -- true indicates to android that the app is taking over back function 
	end
return false  -- Let android process this key normally. 
end


[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,570 posts
  • Corona SDK

Please provide link to original post where you got the code for context.



[TOPIC: post.html]
#3

laurasweet8888

[GLOBAL: userInfoPane.html]
laurasweet8888
  • Contributor

  • 111 posts
  • Corona SDK

@roaminggamer:  

https://forums.coronalabs.com/topic/70345-event-key-back-and-composer/?hl=platformname

 

 

I just had another 2  thought as I try and code this into my game.  

 

If I have this "key" listener taking control during a scene. 

 

1.  What happens between scenes when the listener would be removed as the new scene is loaded?   Seems like there would be a window where a user could click the back button and Android would exit the app.   

 

2.  Let's say my app has a back button and the user presses it and is presented with a popup asking if they really want to go back.  During that popup the user presses the android back button.  I think that is like the ghost busters crossing streams, am I right?   So now we have to keep the two back buttons in sync some how.  



[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,570 posts
  • Corona SDK

@laura,

 

Your First Post

 

1.  Correct re "platform"

 

2.  Wrong. 

2a. Simply set a flag to force it while you're on the simulator, then...

2b. Look at the 'hardware' menu when running simulator.  You can use that to throw a 'back' key event.

 

3.  I think the phases are right the way they are.  You need to catch the DOWN phase or Android will do what it wants.

 

4. Not sure I got this.

 

Your Second Post

 

#1 -  What does your testing show?  I don't think this can happen.

 

#2 - IMHO That user is stupid.... However, it is an interesting thought experiment... what does android do when a native dialog is open and you press back?  I think it closes the dialog and resumes the app.  So this might be handled.  Check and see.

 

These kinds of questions are things you should test and share with us.  I don't know what will happen and would rather you investigate it.

 

Thanks,

Ed



[TOPIC: post.html]
#5

laurasweet8888

[GLOBAL: userInfoPane.html]
laurasweet8888
  • Contributor

  • 111 posts
  • Corona SDK

Thanks as always,   I have started testing and will let you all know what I find.  The first thing I noticed is that I get lots of up and down events for a single key press.  So I need to reconcile how I am ignoring the duplicates.  

 

 

 

3.  I think the phases are right the way they are.  You need to catch the DOWN phase or Android will do what it wants.

 

The code is catching every back event and sending back true.  So Android should be happy.  It only goes back on release.  This of course is subject to personal preference I guess.   The Freebirds game seems to only process the back button on release as well.  

 

 

 

#2 - IMHO That user is stupid.... However, it is an interesting thought experiment... what does android do when a native dialog is open and you press back?  I think it closes the dialog and resumes the app.  So this might be handled.  Check and see.

 

I was thinking that the user is trying to issue back twice but after reading your comment I realize I do this all the time.  When some app asks me a yes/no question I press the back button to indicate no.  I just tested it in your Freebirds game and when you ask if I really want to quit I just press back to get rid of the popup.  Essentially, the same as clicking no.  I know in other apps I do this so I don't have to read the popup, generally they are ads, and just close the dialog box.  



[TOPIC: post.html]
#6

laurasweet8888

[GLOBAL: userInfoPane.html]
laurasweet8888
  • Contributor

  • 111 posts
  • Corona SDK

 

 

1.  What happens between scenes when the listener would be removed as the new scene is loaded?   Seems like there would be a window where a user could click the back button and Android would exit the app.   

 

OK,  I just tested this and when my onKeyEvent listener is removed between scenes and I hit the back button the app exits because there is no listener running and Android's default behavior is to exit the app when back is pressed.  I am currently not using storyboard nor any native dialog boxes.  So maybe the way you guys's apps are coded may be different.  

 

I have noticed that you can have more than one function registered to listen for key events.  So you can set up a listener that always returns true to fool Android and never turn it off and then have listeners that are scene specific which actually do what you want.  

 

Maybe a better way to do it is have one master listener function that can handle each possible scene correctly.  I don't see how I could do that given that I would have to globalize so many variables to make that work.  it would get ugly really fast.  



[TOPIC: post.html]
#7

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,084 posts
  • Corona SDK

I use a variable called backButtonEnabled.  I toggle that whether I want my app to respect the back button or not.  For example I will disable it when loading, etc




[topic_controls]
[/topic_controls]