I want to implement back button in Android, actually already implemented but need fresh ideas and if my implementation is already good share with you guys the logic:D
First of all let me describe how back button behaves in Android if no one will overwrite its behaviour by default:
1-If you are in the root activity and there is no more place to go, and if you press back button the application will close itself.
2-If there are in some child activity over the main menu or in any other window then parent activity, when you press back button the activity(screen) you were in will be closed and you will be returned to your last opened activity.
So in order to implement this one we need to track which screen we are in Gideros:
1-I will create boolean variables for each of my screens like
BoolMainMenu = true
BoolScreen1 = false
BoolScreen2 = false
BoolScreen3 = false
main menu boolean will be true by default hence my program will start with main menu screen:D
2-On each new screen opened by the user i will change the boolean values of the screens . For example if the user will enter screen2 my boolean values will be as follow:
BoolMainMenu = false
BoolScreen1 = false
BoolScreen2 = true
BoolScreen3 = false
This process is so easy to keep track just implement listeners for each screen Added_to_stage, Removed_from_stage and set your boolean values there.
3-On stage i will add event listener for back key (see: Keyboard example under Hardware in Gideros installation folder for it) and in its event function i will check my booleans.
If my main menu boolean is true then
application:exit()
(Or according o your choice just suspend it)
If any other screen is true then first unload all your sprites from stage and then call it like:
stage:addChild(Myscreen1.new())
(Of course i am supposing your each screen is handled in a class logic)
I will try to make a simple example which is using this logic above and share with you guys soon.
Comments
I was doing it a little simpler, all my scenes had a back method, since I already was implementing on screen button to do that, so on scene init I simply added back method as a handler for key event if the key was "back" button.
Basically it was simply adding same method to on screen back button and android back button.
Except of course first screen, which I implemented as application:exit()
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975