Search code examples
androidandroid-fragmentsandroid-dialogfragmentandroid-menu

Menu (and actionbuttons) cleared when showing DialogFragment


When I show a DialogFragment in my application, the Menu (ActionBar) is cleared and doesn't reappear when dismissed. I expect the menu to stay the same when the DialogFragment is shown (since everything behind the Dialog is disabled anyway) but at least to reappear when the Dialog is closed...

The Menuis added in the standard way from my Fragment:

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

And the dialog fragment is also shown in a very standard way. The dialog fragment doesn't contain any wierdness (and no Menu code).

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.addToBackStack(null);
SingleSettingDialogFragment dialog = SingleSettingDialogFragment.newInstance(...);
dialog.show(transaction, "Single"); 

Do does anyone know why this happens or how to fix it?


Solution

  • I'm surprised nobody seems to have had this problem. The problem was transaction.addToBackStack(null);. Since DialogFragments are not pausing the calling Fragment, that fragment shouldn't be added to the back stack either. This also solved problems where the calling fragment is duplicated when pressing back from the Dialog.