开发者

OptionsMenu appears in double

I Have a Fragment inside an Activity. This Activity con开发者_运维技巧tains OptionsMenu.

I use a FragmentTransaction to launch a new instance of the same Fragment class, when I selected the OptionsMenu, it appears in double because it load twice.

How can I do to manage OptionsMenu between the Fragments

Thanks.

PS : Sorry for the English


There is no way such way. You can handle on BackPressed and show your own custom menu as a new fragmnet. Or manage your options menu inside of FragmentTransaction inactivity in dependent of your fragment class and content


I find a solution,

I create the OptionsMenu only in the Activity. I get the OptionsItemsSelected on the Activity too.

When onOptionsItemSelected() occur, I get the current Fragment like this :

FragmentManager fg = getSupportFragmentManager();
Fragment frag = fg.findFragmentById(R.id.fragmentMap);

All my Fragment have the same id but I always get the Fragment on the top. Then, I can call :

frag.onOptionsItemSelected(item);

to the Fragment receive and my UI is correctly update.

I hope It helps.


Try playing with setHasOptionsMenu(boolean) in your Fragment extending classes, if that is set to true the FragmentManager should automatically call your current fragment's onOptionsItemSelected, it set to false you can manually call those callbacks yourself in your Activity's onOptionsItemSelected.

Worked for me... :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜