javaandroidandroid-fragmentsactivity-lifecycleandroid-10.0

Calling onCreateOptionsMenu manually in Android 10


Is there any way to force framework to call my fragment's onCreateOptionsMenu method. What my issue is I am doing some work in onResume() method which is taking some time.

    @Override
        protected void onResume() {
            Log.d(TAG,"onResume");
            super.onResume();
            //some work
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        Log.d(TAG,"onCreateOptionsMenu start");
        menu.add(Menu.NONE,132332,0,"ABCD").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add(Menu.NONE,132332,0,"ABC").setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        Log.d(TAG,"onCreateOptionsMenu end");
        return super.onCreateOptionsMenu(menu);
    }

What I observed is onCreateOptionsMenu is called after some delay(like 1 second) after onResume() method is finished. This is causing a small delay in showing options menu on UI. So, Is there any way to force framework to call my fragment's onCreateOptionsMenu method. I searched about it on internet but didn't find any answer.


Solution

  • You should call invalidateOptionsMenu