Search code examples
androidandroid-actionbarandroid-toolbar

why onPanelCosed is not called when the options menu is dismissed?


I'm setting up the overflow button as below in the subclass of AppCompatActivity

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // For Hamburger menu
        toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        toolbar?.overflowIcon =
            ResourcesCompat.getDrawable(resources, R.drawable.ic_hamberger_menu, null)
}

// Menu icons are inflated just as they were with actionbar
override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    Log.e("Menu", "onCreateOptionsMenu")
    menuInflater.inflate(R.menu.rms_patrol_menu, menu)
    if (menu is MenuBuilder) {
        menu.setOptionalIconsVisible(true)
    }
    return true
}

override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
    toolbar?.overflowIcon =
        ResourcesCompat.getDrawable(resources, R.drawable.ic_about, null)
    if ( featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR) {
        Log.e("Menu", "onMenuOpened")
    }
    return super.onMenuOpened(featureId, menu)
}

override fun onPanelClosed(featureId: Int, menu: Menu) {

    Log.e("Menu", "onPanelClosed")
    super.onPanelClosed(featureId, menu)
}

the onMenuOpened is called each time the menus are shown, but the onPanelClosed is never called when dismiss the menus. Is there anything wrong/missing for the onPanelClosed?


Solution

  • In ToolBarActionBar class have this code

        @Override
        public void onMenuModeChange(MenuBuilder menu) {
            if (mWindowCallback != null) {
                if (mDecorToolbar.isOverflowMenuShowing()) {
                    mWindowCallback.onPanelClosed(AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR, menu);
                } else if (mWindowCallback.onPreparePanel(Window.FEATURE_OPTIONS_PANEL,
                        null, menu)) {
                    mWindowCallback.onMenuOpened(AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR, menu);
                }
            }
        }
    

    first of all the activity must support FEATURE_SUPPORT_ACTION_BAR

    remove layout toolbar

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }
    
        override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menu?:return false
            menuInflater.inflate(R.menu.menu_operate, menu)
            return true
        }
    
        override fun onMenuOpened(featureId: Int, menu: Menu?): Boolean {
            Log.e("Menu", "onMenuOpened")
            return super.onMenuOpened(featureId, menu)
        }
    
        override fun onPanelClosed(featureId: Int, menu: Menu) {
            Log.e("Menu", "onPanelClosed")
            super.onPanelClosed(featureId, menu)
        }
    
    }
    

    and use action bar

    <resources>
    
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!--<item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>-->
    </style>
    

    then you can view the log like this

    2019-12-02 16:53:17.600 14963-14963/ E/Menu: onMenuOpened
    2019-12-02 16:53:20.212 14963-14963/ E/Menu: onPanelClosed
    

    we can custom action over by

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!--<item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>-->
        <item name="android:actionOverflowButtonStyle">@style/CustomOverFlow</item>
    </style>
    <style name="CustomOverFlow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
        <item name="android:src">@android:drawable/ic_menu_add</item>
        <item name="android:background">?android:attr/actionBarItemBackground</item>
        <item name="android:contentDescription">"Lala"</item>
    </style>
    

    This is demo layout R.layout.activity_main

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    without androidx.appcompat.widget.Toolbar if you want use androidx.appcompat.widget.Toolbar see offical developer doc

    I change layout file

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <androidx.appcompat.widget.Toolbar
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/AppTheme"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            >
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hello World!"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </androidx.appcompat.widget.Toolbar>
    </androidx.constraintlayout.widget.ConstraintLayout>
    
     <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!--<item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>-->
        <item name="android:actionOverflowButtonStyle">@style/CustomOverFlow</item>
    </style>
    <style name="CustomOverFlow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
        <item name="android:src">@android:drawable/ic_menu_add</item>
        <item name="android:background">?android:attr/actionBarItemBackground</item>
        <item name="android:contentDescription">"Lala"</item>
    </style>
    
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            setSupportActionBar(my_toolbar)
        }
    
        override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menu?:return false
            menuInflater.inflate(R.menu.menu_operate, menu)
            return true
        }
    
        override fun onMenuOpened(featureId: Int, menu: Menu?): Boolean {
            Log.e("Menu", "onMenuOpened")
            return super.onMenuOpened(featureId, menu)
        }
    
        override fun onPanelClosed(featureId: Int, menu: Menu) {
            Log.e("Menu", "onPanelClosed")
            super.onPanelClosed(featureId, menu)
        }
    
    }
    

    then onPanelClosed can call