Android Question ACToolbar Menu - Activity_CreateMenu called twice

gravel

Member
Licensed User
Longtime User
I've an activity with ACToolbar Menus

I've been testing code that restores state, and everything works as expected on API > 19. On Android 4.4.4 (and probably any API 19 I guess) the restoration of the appropriate menu for the State is being messed up by there being 2 calls to Activity_CreateMenu.

The attached project shows what I'm seeing, 1 call to Activity_CreateMenu on Android 5, 6 & 7 and 2 calls to Activity_CreateMenu on 4.4.4

Does anyone have any idea why this is happening and how to prevent it?

This is the example code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
    #Extends: android.support.v7.app.AppCompatActivity  
#End Region
#If Java

public boolean _onCreateOptionsMenu(android.view.Menu menu) {
    if (processBA.subExists("activity_createmenu")) {
        processBA.raiseEvent2(null, true, "activity_createmenu", false, new de.amberhome.objects.appcompat.ACMenuWrapper(menu));
        return true;
    }
    else
        return false;
}
#End If
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private ToolBar As ACToolBarLight
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Listviewer")
End Sub

Sub Activity_Resume
    Log("Activity_Resume Code End")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#IgnoreWarnings: 11
Sub Activity_CreateMenu(Menu As ACMenu)
    Log("Activity_CreateMenu Start")      
    Menu.Clear
    Dim item As ACMenuItem
    Menu.AddWithGroup2(0, 10, 0, "Menu1", Null)
    Menu.AddWithGroup2(1, 20, 0, "Menu2", Null)
    Menu.AddWithGroup2(2, 30, 0, "Menu3", Null)
End Sub
 

Attachments

  • MenuProb.zip
    4.3 KB · Views: 335
Last edited:

gravel

Member
Licensed User
Longtime User
The Activity has an UltimatelistView (the default view) and a Scrollview which shows the user a detailed view of a ULV entry or a selection panel to change the content the ULV shows, each have their own menus. I populate/repopulate the ULV in Resume and CallSubDelayed a sub that restores the detail view or selection view if required and sets the approproiate menus for the state, this executes after the first CreateMenu

It gets messed up because the second call to CreateMenu I'm seeing on Android 4.4 is executing after my callSubDelayed executes and is resetting the menus to the default state rather than the restored state.

Thanks, no I hadn't seen that example.
 
Upvote 0

gravel

Member
Licensed User
Longtime User
Yes, thank you. I was thinking I could work round it, but it seemed 'not right' that it was getting invoked twice.
 
Upvote 0
Top