Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Which Activity-Screen i am in? - Gideros Forum

Which Activity-Screen i am in?

talistalis Guru
edited September 2012 in General questions
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.

Likes: gorkem

+1 -1 (+1 / -0 ) Share on Facebook

Comments

  • Interesting. Would love to see an example.

    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()
  • john26john26 Maintainer
    I got a bit confused with menus myself and eventually settled on something like
     
    local menu
    local kill
     
    function main_menu(self,event)
      if (not self) or self:hitTestPoint(event.x,event.y) then
     
          if (menu) then
    	 menu:removeFromParent()
          end
     
          if (kill) then
    	 kill:removeFromParent()
             kill=nil
          end
     
          menu=Sprite.new()
          -- fill sprite with children (shapes, bitmaps etc)
     
          local m7=menu:getChildAt(7)    -- this button opens the options menu
          m7:addEventListener(Event.MOUSE_DOWN,menu_options,m7)
      end
    end
     
    function menu_options(self,event)
       if (not self) or self:hitTestPoint(event.x,event.y) then
     
          if (menu) then
    	 menu:removeFromParent()
          end
     
          if (kill) then
    	 kill:removeFromParent()
          end
     
          -- kill set to bitmap "x"
     
          -- touching the x returns to main menu
          kill:addEventListener(Event.MOUSE_DOWN,main_menu,kill)  
     
          menu=Sprite.new()
          -- fill sprite with children (shapes, bitmaps etc) with touch listeners
       end
    end
    So there is only one "menu" with all options in it and one "kill" button (an "x" at the bottom of the screen) which kills the menu and returns to the previous one. Each menu function destroys the previous menu (and kill button) if they are present. You could link the Android back button in place of the kill sprite.
Sign In or Register to comment.