Android Question Doubts using MenuItem with callsubdelayed

Ms Boar

New Member
Licensed User
Hello everyone, I have a tiny doubt about the correct sintax with MenuItem.

Im tryin to simplify my life a bit, with the menus, but today its not my lucky day.

So these are my methods

B4X:
Sub Activity_Create(FirstTime As Boolean)
     Activity.LoadLayout("Login")  
    InputLogin1.Hint = "Name"   
    Activity.Title ="Login"
    AuxCode.Menu_Charge(Activity,Null) ''My aux methods
End Sub
''my menu constructor, in an Auxiliar module called auxCode
B4X:
Sub  Menu_Charge(Act As Activity, status As String)
    Act.AddMenuItem("About", about_Click)
(...)
EndSub
'The click Method
Sub about_Click
    Msgbox("Version X", "MyAPP")
End Sub

The behaviuour with this code block is that when activity starts the msgbox is shown ( so if its called 7 times, the msgbox its shown 7 times)

So, Surfing through the forum, I´ve see that the ''best way or correct'' syntax is with callsubdelayed, but when I write :
B4X:
Act.AddMenuItem("About", CallSubDelayed(something, "about_Click"))

1st Something needs to be a component, but i do not understand which component should be there.

Anything i write, :: B4A tells that I cant assing this to an empty value.



My question is, if is not a syntax error ( that i beg it is... ) , it is possible to create the menus by this way, or it is preffered to use a button and a spinner by my own way.

Kind Regards,
MsB
 

Ms Boar

New Member
Licensed User
Hi,

Maybe Code its not complete

Even using

B4X:
MsgboxAsync("Version X", "MyAPP")

Message appears when activities start. At the start of each activity, where my sub menu_charge is called, the msgboxasync is shown , then in the menu, at click in the row do nothing, the msgbox does not appear.


What im trying to solve : Shown distincts menus depending on the user.

My problem is that the msgboxAsycn is shown when activity starts and do not respond when clicked.



Surfing forum i ve found the most similar... but do not solve my trouble

https://www.b4x.com/android/forum/threads/addmenuitem-and-android-7.71542/

Where..

This is not a bug. You shouldn't show modal dialogs in the menu click event (see the documentation). Use CallSubDelayed to call a method from the menu event and show the msgbox there.


Acording to this, if I should use callsub delayed, when I try shows an error

"cant assing to empty value"

So i have the doubt that if it is need to use callsubdelayed, which component as object should be?
B4X:
 'callsubdeladey(object, sub, arguments)
Act.AddMenuItem("TITLE",CallSubDelayed2("Which Object should be here?", "about_click",Null ( no arguments))

Which Is the best way/correct way/ correct sintax to use application menu to interact with?

Thank you!
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
This is not a bug. You shouldn't show modal dialogs in the menu click event (see the documentation). Use CallSubDelayed to call a method from the menu event and show the msgbox there.
The way I interpret this is that you should have
B4X:
    Act.AddMenuItem("About", about_Click)
and then do
B4X:
Sub about_Click
          CallSubDelayed(Me, "about_showsomething")
End Sub

Sub about_showsomething
'do what you need to be done
End Sub
Notice I use Me in the CallSubDelayed, since the sub in this example is in the same class module code module. If the sub is in a different code module/Activity/Service, then use that code module's/Activity's/Service's name (Main for example). See: https://www.b4x.com/android/forum/t...teract-between-activities-and-services.18691/
 
Upvote 0

Ms Boar

New Member
Licensed User
Thanks for your answer, the key is that about_Click is on a module, so do not support Me keyword,

What im feeling is that it is need to code in an explicit form each menu at each activity, and it is not posible to set a general menu in a code module, that creates each menu depending on the activity.

I will keep on trying, its just a felling, cannot be true. :D

Thank you all!
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
module, so do not support Me keyword
Huh? Do a log(Me) on a code module. It should work (warning: not an expert. Time to experiment).
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Ugh. Like I said, not an expert (it works in B4J, so odd that it does not work in B4A). Put the called sub in another module and try
B4X:
CallSubDelayed(TheOtherCodeModule, "about_showsomething")
Note: Ignore. This does not work in B4A (does in B4J, not B4A)
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I dont think it will help here at all.
Using callsubdelayed the sub is called after activity_create finishes. You only can add menuitems in activity create.

Exception when you are using AppCompat.
 
Upvote 0
Top