Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does onCreateOptionsMenu run multiple times?

For some reason, my onCreateOptionsMenu and onPrepareOptionsMenu run twice (checked with a log input on the start of both methods). This happens for multiple fragments that I have, including some that are very basic (just inflating the menu, nothing else).

This is one of the onCreateOptionsMenus that has this issue:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.actionbuttons_add_removeall, menu);
    optionsMenu = menu;
}

What could cause these methods to be called multiple times (mostly twice)?


Update

I found out this is being caused (in some way) by the RecyclerView I'm using. All the views that are having this issue use a RV, the view I mentioned before that didn't have this issue, indeed doesn't. With this new info, which part of the RV could possibly be posing this issue?

Update2

I found out that I call .invalidateOptionsMenu() in the getItemCount() method in the Adapter. I thought that this would call onPrepareOptionsMenu(), but reading the docs, it seems like it calls onCreateOptionsMenu(). I'm probably getting onCreate..() and onPrepare..() reversed here, gonna check that out now.

Update3

I have just realized that I invalidate the options menu in my RecyclerViewAdapter, in the getItemCount() method, which obviously runs when the fragment is first created.

@Override
public int getItemCount() {
    int tableSize = getTableSizeMethod();

    if (tableSize < 1) {
        if (!AppManagerFragment.hideDeleteAllButton) {
            AppManagerFragment.hideDeleteAllButton = true;
            ((Activity) context).invalidateOptionsMenu();
            return 0;
        }
    } else {
        if (!AppManagerFragment.hideDeleteAllButton) {
            AppManagerFragment.hideDeleteAllButton = false;
            ((Activity) context).invalidateOptionsMenu();
            return tableSize;
        }
    }
}
like image 405
Timmiej93 Avatar asked Aug 08 '16 19:08

Timmiej93


People also ask

Why does my query run multiple times?

When refreshing in Power Query, there's a lot done behind the scenes to attempt to give you a smooth user experience, and to execute your queries efficiently and securely. However, in some cases you might notice that multiple data source requests are being triggered by Power Query when data is refreshed.

Why does my flow run twice in one Test?

It runs great for me in the dev environment. It runs twice, because an update of Opportunities in the flow triggers it to run again. The field that was updated in the first run is examined and the flow is aborted on that second run. The problem is, something is triggering the flow multiple times in the test instance.

Why does the flow run twice in Salesforce?

It runs twice, because an update of Opportunities in the flow triggers it to run again. The field that was updated in the first run is examined and the flow is aborted on that second run.

How can I avoid multiple requests from the folding layer?

Sometimes Power Query’s folding layer may generate multiple requests to a data source, based on the operations being performed downstream. In such cases, you might avoid multiple requests by using Table.Buffer.


2 Answers

These methods will be called whenever fragment either first creates or becomes visible and onResume, because onCreate is called even before onCreateView where all non ui things get initialized and calls to Activity is made. Hope it is clear why these two are calling super() of Parent Activity multiple times.

Now what to do to stop fragment from calling activity's onCreateOptionsMenu to inflate Menu layout again n again, declare this inside onCreate (overridden method) inside fragment.

setHasOptionsMenu(false);

If you want to have menu at activity as well but different menus for fragments then do

menu.clear(); 

Instead of calling super() from fragment's onCreateOptionsMenu()

If you just want to disable menu at Mainactivity try return false or simply remove onCreateOptionsMenu() from MainActivity.

As you said, launcher activity is a fragment so you want to disable menu for this fragment, so do something like this in this fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

Then from onCreateOptionsMenu() inside same fragment disable and hide those menuItem.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
}

Even if this doesn't help, then try finding separate items of menu and disable them and set their visibility to false.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (menu != null) {
       menu.findItem(R.id.action_abc).setVisible(false);
       menu.findItem(R.id.action_xyz).setVisible(false);
    }
}

Hope i helped . !

like image 137
mfaisalhyder Avatar answered Sep 28 '22 01:09

mfaisalhyder


This one was my own fault. I was invalidating the options menu in the getItemCount() method of my RecyclerViewAdapter, which obviously runs when the fragment is initiated. You can check out the question for the code that contains my error. Thanks for the help/suggestions all.

like image 20
Timmiej93 Avatar answered Sep 28 '22 00:09

Timmiej93