Search code examples
androiddialogfragmentoptionmenu

Option menu in DialogFragment


I have two fragments one is list fragment and the other one i'm using it as dialog once and as fragment my problem here with option menu, in list fragment there's an item menu given show always action and in dialog fragment i don't want this item to be visible and i don't want option menu to be visible too, i have tried to

setHasOptionMenu(false)

but it didn't work and tried to setHasOptionMenu(true) and clear all items in menu but it didn't work neither.

Also, my parent activity doesn't have any code for option menu. here's my code:

@SuppressLint("InflateParams")
public class FormGeneratorActivity extends DialogFragment {
Control mControl;
boolean doChangeTitle;
private boolean isDialog;
public static int CreatedNum = 0;


public static FormGeneratorActivity getInstance(Control mControl, boolean doChangeTitle) {
    FormGeneratorActivity frag = new FormGeneratorActivity();
    Bundle b = new Bundle();
    b.putParcelable("control", mControl);
    b.putBoolean("changeTitle", doChangeTitle);
    frag.setArguments(b);
    return frag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        doChangeTitle = getArguments().getBoolean("changeTitle");
    }
    setHasOptionsMenu(false);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.clear();
    activity.invalidateOptionsMenu();
    super.onPrepareOptionsMenu(menu);

}

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



public void setDoChangeTitle(boolean doChangeTitle) {
    this.doChangeTitle = doChangeTitle;
}

View view;
boolean isInflated = false;
ParentActivity activity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (view == null) {
        mControl = getArguments().getParcelable("control");
        view = inflater.inflate(R.layout.form_generator_fragment, container, false);
        container = (LinearLayout) view.findViewById(R.id.cont);
        isInflated = true;
    } else {
        if (view.getParent() != null)
            ((ViewGroup) view.getParent()).removeAllViews();
        isInflated = false;
    }

    return view;
}

String headerPageTitle = null;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    activity = (ParentActivity) getActivity();
    activity.getSupportActionBar().invalidateOptionsMenu();

        CreatedNum++;

}

@Override
public void onResume() {
    super.onResume();
    activity.setPageTitle(headerPageTitle);
    setHasOptionsMenu(false);
    activity.getSupportActionBar().invalidateOptionsMenu();

}


public void isDialog(boolean b) {
    isDialog = b;
}

}


Solution

  • i found out that dialog fragment only needs time to know that the option menu doesn't exist any more so i had to post delay using handler before i start the fragment