ListFragment在prepareOptionsMenu的onCreate之前调用。为什么以及如何修复/旁路? [英] ListFragment onPrepareOptionsMenu called before onCreate. Why and How to Fix / Bypass?

查看:245
本文介绍了ListFragment在prepareOptionsMenu的onCreate之前调用。为什么以及如何修复/旁路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有FavoritesList延伸GalleryList延伸ListFragment:

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);
        }
    }
}

下面的问题是:在prepareOptionsMenu被称为的的的onCreate(我的初始化listAdapter)加载此片段时,它的不是的再次呼吁前选项​​菜单中显示的第一次!

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!

的<一个href="http://developer.android.com/reference/android/support/v4/app/Fragment.html#on$p$ppareOptionsMenu%28android.view.Menu%29">Fragment文档是完全错误的,当它在prepareOptionsMenu声称之称的 的菜单显示右前的每次的它显示。

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。我使用的是Android的支持库(V4)。任何想法?

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

推荐答案

所以,愚蠢的,因为这是,这里是一个有效的解决方法:

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);
        }
    }
}

基本上,我已经对prepareOptionsMenu在第一次运行时抢下的选项菜单,然后再次叫了一次listAdapter已初始化。

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

编辑:显然没有检查是否optionsMenu为null,这将打破某些手机。我应该意识到。

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

这篇关于ListFragment在prepareOptionsMenu的onCreate之前调用。为什么以及如何修复/旁路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆