0

I have a refresh menu item in action bar. I can expand action view (== show progress bar in actionbar) like this:

menuItem.setActionView(R.layout.action_view_refresh);
menuItem.expandActionView();

I do this when user presses on menu item in action bar.

But now i want to show it automatically when app starts and my fragment shows (in fragment's onCreateView). The problem is that function

public boolean onCreateOptionsMenu(Menu menu)

, where my menuItem is initialized, is called after onCreateView.

How can i initialize menuItem before onCreateOptionsMenu so i can expand actionView in onCreateView ?

DixieFlatline
  • 7,895
  • 24
  • 95
  • 147
  • 1
    Why not "expand actionView" in `onCreateOptionsMenu()` instead? What is magical about doing this in `onCreateView()`? – CommonsWare Jun 10 '13 at 13:37
  • This expanded action view shouldnt be always on, only when i make some http request. But your idea is ok, i handled it this way now. Tnx – DixieFlatline Jun 11 '13 at 13:50

1 Answers1

2

As a general rule, you use supportInvalidateOptionsMenu to trigger a refresh or some action on menuitems. All the updating work should be put inside onPrepareOptionsMenu(). In the case you want fragment to pull the trigger, first you can do something like this in activity level:

// YourActivity
boolean trigger = false;

public void invalidate(boolean isTrigger) {
    trigger = isTrigger;
    supportInvalidateOptionsMenu();
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    if(trigger) {
        MenuItem menuitem = menu.findViewById(R.id.menu_id);
        menuItem.setActionView(R.layout.action_view_refresh);
        menuItem.expandActionView();

        // reset trigger
        trigger = false;
    }
    return super.onPrepareOptionsMenu(menu);
}

Then, in your fragment createView you can just call the invalidate method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // trigger the actionview   
    ((YourActivity) getActivity()).invalidate(true);
}
Neoh
  • 15,906
  • 14
  • 66
  • 78