Я делаю мобильное приложение на Android с JAVA, используя DialogFragment с панелью инструментов.
Я хочу условно скрыть пункт меню.
Меню
<?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/action_favorite"
android:title="favorite"
android:icon="@drawable/ic_favorite_white_off_24dp"
app:showAsAction="always" />
<item
android:id="@+id/action_delete"
android:title="delete"
android:icon="@drawable/ic_delete_white_24dp"
app:showAsAction="always" />
<item
android:id="@+id/action_save"
android:title="save"
app:showAsAction="always" />
</menu>
Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:paddingStart="6dp"
android:paddingEnd="16dp"
app:contentInsetStartWithNavigation="0dp"
app:navigationIcon="@drawable/ic_close_black_24dp" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- EditText's and buttons ..... -->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
DialogFragment
public class MyDialogFragment extends DialogFragment implements Toolbar.OnClickListener, Toolbar.OnMenuItemClickListener {
public static final String TAG = "my_dialog";
private Toolbar toolbar;
public static MyDialogFragment display(FragmentManager fragmentManager) {
NewPersonDialogFragment exampleDialog = new NewPersonDialogFragment();
exampleDialog.show(fragmentManager, TAG);
return exampleDialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(NewPersonDialogFragment.STYLE_NORMAL, R.style.AppTheme_FullScreenDialog);
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
dialog.getWindow().setWindowAnimations(R.style.AppTheme_Slide);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.dialog_fragment_new_person, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
toolbar = view.findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.new_person_dialog_menu);
toolbar.setNavigationOnClickListener(this);
toolbar.setOnMenuItemClickListener(this);
toolbar.setTitle("Add new");
}
@Override
public void onClick(View v) {
// This is working fine
}
@Override
public boolean onMenuItemClick(MenuItem item) {
// This is working fine
switch (item.getItemId()) {
case R.id.action_favorite:
return true;
case R.id.action_delete:
return true;
case R.id.action_save:
return true;
default:
return false;
}
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
// Removing item doesn't work!
menu.removeItem(R.id.action_delete);
}
}
Я открываю DialogFragment следующим образом:
MyDialogFragment.display(getActivity().getSupportFragmentManager());
Когда я открываю DialogFragment, элемент меню не скрывается.
Как говорится в других темах, я пробовал:
setHasOptionsMenu(true)
в методе onActivityCreated
. getActivity().invalidateOptionsMenu()
в методе onPrepareOptionsMenu
.
Моя цель - скрыть / удалить пункт меню в зависимости от условия, когда пользователь открывает DialogFragment.