跳到主要內容

Android 如何在 Fragment 執行 onCreateOptionsMenu

Activity執行 Fragment預設並不會執行onCreateOptionsMenu,因此為了讓每個Fragment都有不同的OptionsMenu時,就得要執行setHasOptionsMenu(true);
簡易範例如下

...

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

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

...

這樣就可以執行到onCreateOptionsMenu的功能了

留言

這個網誌中的熱門文章

What is phpize

What is phpize According to the PHP official document : The phpize command is used to prepare the build environment for a PHP extension. If you need to build such an extension that from github or another code repositories, you can use  build tools to perform the build manually.