ListFragment onPrepareOptionsMenu called before onCreate. Why and How to Fix / Bypass?

点点圈 提交于 2019-12-30 08:34:29

问题


Okay, so I've got FavoritesList extending GalleryList which extends ListFragment:

public static class FavoritesList extends GalleryList {

    public static FavoritesList newInstance(int page) {
        FavoritesList list = new FavoritesList();

        Bundle args = new Bundle();
        args.putInt("page", page);
        list.setArguments(args);

        return list;
    }

    @Override
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);

        Cursor cursor = dbHelper.getGalleries(fav, preferences.getString("sort"+fav, "date desc"));
        listAdapter = new GalleryListAdapter(activity, cursor);
        setListAdapter(listAdapter);
    }

    ...

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.add(Menu.NONE, 0, 8, "Remove All");
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        //listAdapter is null the first time this is called...

        if (listAdapter != null && listlistAdapter.getCount() == 0) {
            menu.findItem(R.id.filter).setEnabled(false);
            menu.findItem(0).setEnabled(false);
        }
        else {
            menu.findItem(R.id.filter).setEnabled(true);
            menu.findItem(0).setEnabled(true);
        }
    }
}

Here is the problem: onPrepareOptionsMenu is called before onCreate (where I initialize listAdapter) when loading this Fragment, and it isn't called again before the options menu is shown for the first time!

The Fragment documentation is simply wrong when it claims onPrepareOptionsMenu "is called right before the menu is shown, every time it is shown."

p.s. I'm using the Android Support Library (v4). Any ideas?


回答1:


Try calling invalidateOptionsMenu() on your onCreate(). Make sure to check if your list adapter is null on onPrepareOptionsMenu().




回答2:


So, as silly as this is, here is a functioning workaround:

public static class FavoritesList extends GalleryList {

    Menu optionsMenu;

    ...

    @Override
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);

        Cursor cursor = dbHelper.getGalleries(fav, preferences.getString("sort"+fav, "date desc"));
        listAdapter = new GalleryListAdapter(activity, cursor);
        setListAdapter(listAdapter);

        if (optionsMenu != null) {
            onPrepareOptionsMenu(optionsMenu);
        }
    }

    ...

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        optionsMenu = menu;

        if (listAdapter != null && listAdapter.getCount() == 0) {
            menu.findItem(R.id.filter).setEnabled(false);
            menu.findItem(0).setEnabled(false);
        }
        else {
            menu.findItem(R.id.filter).setEnabled(true);
            menu.findItem(0).setEnabled(true);
        }
    }
}

Basically I've grabbed the options menu during the first run of onPrepareOptionsMenu, then called it again once listAdapter has been initialized.

edit: evidently without checking if optionsMenu is null, this will break on certain phones. I should have realized.



来源:https://stackoverflow.com/questions/12374816/listfragment-onprepareoptionsmenu-called-before-oncreate-why-and-how-to-fix-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!