Domanda

I Have a Fragment inside an Activity. This Activity contains 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

È stato utile?

Soluzione 2

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.

Altri suggerimenti

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

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... :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top