0

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.

Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49
Vivek Mangal
  • 532
  • 1
  • 8
  • 24
  • 1
    "What my issue is I am doing some work in onResume() method which is taking some time." -- move that work to a background thread. If `onResume()` takes longer than 1ms, "you're doing it wrong". – CommonsWare Oct 11 '19 at 11:05

2 Answers2

2

You should call invalidateOptionsMenu

Wirling
  • 4,810
  • 3
  • 48
  • 78
  • invalidateOptionsMenu() will work only when menu is created once. So it is not working. What I want is to be my options menu created early. Once onCreateOptionMenu is called, then I can call invalidateOptionsMenu. – Vivek Mangal Oct 11 '19 at 10:06
  • The problem is that you're doing too much loading in the main thread. You should consider using an [AsyncTask](https://developer.android.com/reference/android/os/AsyncTask) for that. – Wirling Oct 11 '19 at 10:14
  • Don't know why my answer is being downvoted. This is really the answer to the question: `So, Is there any way to force framework to call my fragment's onCreateOptionsMenu method`. I can't help it that the real problem isn't clearly mentioned in the original question. – Wirling Oct 11 '19 at 10:31
  • @VivekMangal Probably not, but it would help if you show us what you're doing in `onResume` that is taking so long. – Wirling Oct 11 '19 at 11:09
-1

you should doing you long job in a backgroud thread

SebastienRieu
  • 1,443
  • 2
  • 10
  • 20