Search code examples
androidandroid-layoutandroid-fragmentsandroid-dialogfragment

Android ActionBar overflow menu options not working when fragment visible


I have a dynamically visible transparent ActionBar in my app (using v4 compatibility) that appears and disappears when the user long presses on the screen. This is working fine, and I'm able to use the ActionBar to execute my actions via public boolean onOptionsItemSelected(MenuItem item)

However, when I display a DialogFragment on screen, only the visible (those not in overflow) ActionBar actions fire onOptionsItemSelected. The overflow menu appears as before but I cannot click on them. They act like they are disabled.

The DialogFragments are setup so that background motion events trigger

final Window w = getDialog().getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

My ActionBar xml isn't too complicated, but here it is.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:showAsAction="ifRoom" android:id="@+id/settings" android:title="@string/settings" android:icon="@android:drawable/ic_menu_preferences"/>
    <item android:showAsAction="ifRoom" android:id="@+id/history" android:title="@string/menu_history" android:icon="@drawable/ic_menu_archive"/>
    <item android:showAsAction="ifRoom" android:id="@+id/new_game" android:title="@string/new_game" android:icon="@android:drawable/ic_menu_add"/>
    <item android:showAsAction="ifRoom" android:id="@+id/stats" android:title="@string/menu_stats" android:icon="@android:drawable/ic_menu_agenda"/>
    <item android:showAsAction="ifRoom" android:id="@+id/help" android:title="@string/help" android:icon="@android:drawable/ic_menu_help"/>
</menu>

Other than that, I'm not doing anything too strange in my app. Any ideas why the overflow menu fails to work when a DialogFragment is displayed?

My DialogFragment class calls setHasOptionsMenu(true) and I can see onCreateOptionsMenu(Menu menu, MenuInflater inflater) being called, but my onOptionsItemSelected doesn't get called for the overflow items.

EDIT I decided to throw away the ActionBar implementation and use a Navigation Drawer slider for my app. After I implemented everything, I ran into the same problem. With no DialogFragments visible, I can use the Navigation Drawer just fine and make selections. However when my DialogFragment is visible, I can drag the drawer in and out, but I am unable to make any selections on the ListView.

Here's my code for the child dialog. public class CChildDialog extends DialogFragment {

public interface DialogDismissHandler {
    abstract public void onDismiss(CChildDialog dlg);
}
protected Game m_game;
protected DialogDismissHandler m_dismissListener;

public CChildDialog() {
    super();
}

public boolean isDialogShowing() {
    Dialog d = getDialog();
    if (d != null)
        return d.isShowing();
    else
        return false;
}

public void setGame(Game g) {
    m_game = g;
}

public void setOnDismissHandler(DialogDismissHandler dismissListener) {
    m_dismissListener = dismissListener;
}

public void onDismiss(DialogInterface dlg) {
    super.onDismiss(dlg);

    if (m_dismissListener != null) {
        m_dismissListener.onDismiss(this);
        m_dismissListener = null;
    }

    m_game = null;
}

public void onPreferencesChanged(Game game) {

}

@Override
public void onStart() {
    final Window w = getDialog().getWindow();

    w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

    WindowManager.LayoutParams lp = w.getAttributes();

    if (m_game.isAdVersion()) {
        // move it up 25 pixels
        lp.y = -25;         
    }
    // check for super small screens
    if (UIUtil.GetWindowSize(m_game.getWindowManager()).y < 400) {
        lp.y -= 30; // move it up a little more
    }

    super.onStart();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(DialogFragment.STYLE_NO_TITLE,R.style.Theme_CustomDialog);
}

And here is my custom theme

<style name="Theme.CustomDialog" parent="@android:style/Theme.Holo.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowNoTitle">true</item>
</style>

Solution

  • The answer is to also add the FLAG_NOT_FOCUSABLE LayoutParams flag. Adding this allows full use of the overflow menu when a DialogFragment is visible.