Menu Button problems

BasicBert

Member
Licensed User
Longtime User
Hi guys.

I'm quite a :sign0104: on B4A but I'm learning fast. At least I get things done now on Android with B4A as opposed to using java etc.

I have been using the possibility to add menu items in my project. Although I only really used one MenuItem I added three of them in my program(s).
This seemed to work well for some versions of my app, but without modifying the code that has to do with defining the menu items and then acting to them, it suddenly doesn't work anymore.

Now when I push the Menu, the app automatically and immediately closes.
These lines are used to define the menu items :

B4X:
'MenuItem Stop
   Activity.AddMenuItem("Stop", "mnuStop")
'MenuItem Open
   Activity.AddMenuItem("Open", "mnuOpen")
'MenuItem Instellingen
   Activity.AddMenuItem("Instellingen", "mnuInstellingen")

And these lines should deal with pushing the menu and choosing an item :

B4X:
Sub mnuStop_Click
   Dim Answ As Int
   Answ = Msgbox2("Wilt U afsluiten?", "A T T E N T I E", "Ja","", "Nee", LoadBitmap(File.DirAssets, "AGlogo.jpg"))
   If Answ = DialogResponse.POSITIVE Then
      Activity.Finish
   End If
   lblMessage.Color = Colors.Red   
End Sub
Sub mnuOpen_Click
   lblMessage.Color = Colors.Yellow
End Sub
Sub mnuInstellingen_Click
   lblMessage.Color = Colors.White
   Dim Answ As Int
   Answ = Msgbox2("Weergeven indien niet voorradig?", "A T T E N T I E", "Ja","?", "Nee", LoadBitmap(File.DirAssets, "AGlogo.jpg"))
   If Answ = DialogResponse.POSITIVE Then
      ShowNulResterend = True
      VulListView
   Else
      If Answ = DialogResponse.NEGATIVE Then
         ShowNulResterend = False
         VulListView
      End If
   End If
End Sub

I have been trying to move the code over the module, but the place does not seem to be important.
Using debug I can follow the initialization-code, but the actual response-code will never execute. No use to set a breakpoint there, it never gets there anyway.

Anyone has a clue?:sign0163:

BasicBert.
 

BasicBert

Member
Licensed User
Longtime User
Strange how things go when you try to explain what's going wrong....
You get to get things in your mind in order again and get to see things from another perspective.

By looking over the code, I crossed another sub that at first wasn't obvious that it could interfere, but after some more trial and error I saw that it indeed was!

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   Dim Answ As Int
   If KeyCode = KeyCodes.KEYCODE_BACK Then
   Answ = Msgbox2 ("Wilt U afsluiten?", "A T T E N T I E", "Ja","", "Nee", LoadBitmapSample(File.DirAssets, "AGlogo.jpg", LogoBreedte, LogoHoogte))
      If Answ = DialogResponse.NEGATIVE Then
         Return True
      End If
   End If
   Activity.Finish
   Return True
End Sub

This sub was wrong by acting on any keycode and close the app with Activity.Finish, except for a BACK-key. Only if the BACK-key was pressed, it would ask to close (afsluiten?), otherwise it would close immediately by 'Activity.Finish'. So my menu items never got displayed and processed !

I now have changed this sub to

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   Dim Answ As Int
   If KeyCode = 82 Then Return False   'Menu key pressed   
   If KeyCode = KeyCodes.KEYCODE_BACK Then
      Answ = Msgbox2 ("Wilt U afsluiten?", "A T T E N T I E", "Ja","?", "Nee", LoadBitmapSample(File.DirAssets, "AGlogo.jpg", LogoBreedte, LogoHoogte))
      If Answ = DialogResponse.POSITIVE Then
      '   Activity.Finish
         ExitApplication
         Return True
      End If
   Else
      Return True         'Do nothing if other keys.
   End If   
End Sub
and it works!

:BangHead:
I'm happy that I could solve this myself and even quicker than I thought was possible. But I also realize that asking for help is one thing, giving a good response is quite difficult, as I can see that this could not have been solved by looking at the code I had given.
So thanks to anyone who replied. Maybe some other people can also learn from this question.
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
:signOops:
Still some problems with the code listed above.
I have tested again and modified/cleaned up the code so now it is working as I expected.
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   Dim Answ As Int
   If KeyCode = 82 Then Return False   'Menu key pressed   
   If KeyCode = KeyCodes.KEYCODE_BACK Then
      Answ = Msgbox2 ("Wilt U afsluiten?", "A T T E N T I E", "Ja","", "Nee", LoadBitmapSample(File.DirAssets, "AGlogo.jpg", LogoBreedte, LogoHoogte))
      Log(Answ)
      Log(DialogResponse.POSITIVE)
      If Answ = DialogResponse.POSITIVE Then
      '   Activity.Finish
         ExitApplication
      End If
      Return True
   End If   
End Sub

After ExitApplication no code will be executed, so the Return True there has no use. Also the If's were not coded correctly.
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
You should normally not call ExitApplication. Instead you should call Activity.Finish.
Erel, I appreciate your answer. The reason I use ExitApplication is stated in another thread : http://www.b4x.com/forum/basic4android-updates-questions/12834-firsttime-activity_create.html. I may not know of all the drawbacks of using ExitApplication, but it works like this. The app has changed a lot since then, so I might test if Activity.Finish now is still giving me problems.

@ admac321 : you're right! The problem is always where you don't expect it to be. Otherwise we wouldn't need this forum. And you don't always want to show your :sign0104: code :eek:
 
Upvote 0
Top