1

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;
}

}

Hanaa Mohamed
  • 107
  • 2
  • 11

4 Answers4

1

Option menu in DialogFragment(Fragment) with Search(SearchView) action:

MenuDialogFragment.class

public class MenuDialogFragment extends DialogFragment {

    Toolbar toolbar;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.custom_dialog_layout, container, false);

        toolbar = view.findViewById(R.id.custom_dialog_layout_toolbar);

        toolbar.inflateMenu(R.menu.menu_custom_dialog);

        Menu menu = toolbar.getMenu();
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_searchview).getActionView();

        // DO whatever you need with menu items

        return view;
    }
}

menu_custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/menu_searchview"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"
        />
</menu>

custom_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.v7.widget.Toolbar
    android:id="@+id/custom_dialog_layout_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    />
</LinearLayout>
researcher
  • 1,758
  • 22
  • 25
0

you can try to set parent fragment like: fragment.setTargetFragment(getSupportFragmentManager().findFragmentById(R.id.frame), 1);

and in child fragment you perform (in onAttach/onDetach methods) getTargetFragment().setMenuVisibility(true);

0

Maybe try to use one of the dialog themes when you call this fragment as dialog. To do this you can use this constructor:

AlertDialog.Builder(Context context, int theme)

ie

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)

Some more explanation: Theme not applying to DialogFragment on Android

Community
  • 1
  • 1
Kacper Wolkowski
  • 1,517
  • 1
  • 16
  • 24
-1

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

Hanaa Mohamed
  • 107
  • 2
  • 11
  • You shouldn't use any time delayed code in such a way, as you can't predict that it will work for every device type. – Chucky Feb 27 '17 at 14:46