onCreateOptionsMenu called after onResume on JB 4.2
Asked Answered
I

1

22

I have the following fragment in my application:

public class MyFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
  private MenuItem refresh = null;

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

    //.....

    // NPE here
    refresh.setActionView(R.layout.indeterminate_progress_action);
    getActivity().getSupportLoaderManager().initLoader(0, null, this);
  }

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.refresh_menu, menu);
    refresh = (MenuItem) menu.findItem(R.id.menu_item_refresh);
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
    refresh.setActionView(null);
  }
}

Basically when the user starts the Activity I want to display a progress spinner, in the actionbar, until the loader completes. Prior to Android 4.2 everything worked fine. On Android 4.2 I get the following error:

11-24 13:37:14.811: E/AndroidRuntime(17850): Caused by: java.lang.NullPointerException
11-24 13:37:14.811: E/AndroidRuntime(17850):    at com.MyApp.library.fragments.MyFragment.onActivityCreated(TabFragment.java:65)

After adding some break point I realized onCreateOptionsMenu was getting called after onActivityCreated. Is there another life cycle event I can use? Is there a better was to achieve the desired effect?

Illstarred answered 24/11, 2012 at 18:43 Comment(6)
Um, why not move your setActionView() call to onCreateOptionsMenu(), then? And perhaps do it conditionally depending upon whether or not the load is done?Digital
Kinda works. Seems like onCreateOptionsMenu gets called after onLoadFinished too. Since I have tabs with multiple loaders hasRunningLoaders isn't useful in determining if load has finished. Guess I will have to use a Boolean value to track the state.Illstarred
Android issue: code.google.com/p/android/issues/detail?id=39721Illstarred
Can you post full error? What line of code is 65? Also can you not just check for != null before using the object, I have fixed similar problems in onCreateOptionsMenu before.Euphuism
https://mcmap.net/q/658369/-android-oncreateoptionsmenu-called-twice-when-restoring-state maybe this is your problem .. ?Carswell
I have Same problem, onCreateOptionsMenu gets called after onResume on MarshmallowFruiter
B
4

I've used onPrepareOptionsMenu in this way to achieve what you are trying to do. (not tested but should give you an idea.)

private boolean mIsLoading = true;

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    refresh = (MenuItem) menu.findItem(R.id.menu_item_refresh);
    if (!mIsLoading) {
        refresh.setActionView(null);
    }
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
    mIsLoading = false;
    getActivity().invalidateOptionsMenu();
}
Brough answered 20/6, 2013 at 17:46 Comment(1)
How come onPrepareOptionsMenu method return void; I think I just saw a pussy cat; scared my freaking mindStorage

© 2022 - 2024 — McMap. All rights reserved.