问题描述
我的应用程序的结构,一个带有使用导航组件的片段的活动。 在我的 HomeFragment 的工具栏中,有一个带有搜索图标作为菜单的工具栏。这个点击的图标会导致搜索活动。 HomeFragment
浏览我的应用程序并将片段更改为 ProfileFragment,我有机会查看我的列表并单击其中一个打开另一个片段以显示该列表的内容 ProfileFragment、ShowListContent。
现在是魔法
如果我点击工具栏图标来更改我的列表的描述,一切都是完美的,我们很高兴但是......
如果我回到 HomeFragment,工具栏中的图标不再相同,我也无法进入 SearchActivity。
我该怎么做才能避免这个问题?
代码时间
public class HomeFragment extends Fragment{
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_home,container,false);
initToolbar(view);
//other code
return view;
}
private void initToolbar(View view) {
Toolbar toolbar = view.findViewById(R.id.app_bar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.menu_main_toolbar);
}
@Override
public void onCreateOptionsMenu(@NotNull Menu menu,@NotNull MenuInflater
inflater) {
super.onCreateOptionsMenu(menu,inflater);
menu.clear();
inflater.inflate(R.menu.menu_main_toolbar,menu);
}
@Override
public boolean onoptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_search) {
Intent intent = new Intent(getActivity(),SearchActivity.class);
startActivity(intent);
}
return true;
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#141a32"
tools:context=".fragments.HomeFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/app_bar"
style="@style/toolbar"
app:menu="@menu/menu_main_toolbar"
app:titleTextAppearance="@style/Toolbar.TitleText"
app:titleTextColor="@color/white" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.nestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout">
//other code
</androidx.core.widget.nestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
ShowContentListFragment
public class ShowContentListFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragments_layout_lists,false);
setHasOptionsMenu(true);
getDataFromSafeArgs();
initWidgets(view);
initToolbar();
return view;
}
private void initToolbar() {
toolbar.setVisibility(View.VISIBLE);
toolbar.setNavigationIcon(R.drawable.ic_friend_list_back_button);
toolbar.inflateMenu(R.menu.menu_profile_edit_list);
toolbar.setNavigationOnClickListener(v -> {
Navigation.findNavController(getView())
.navigate(R.id.action_showContentListFragment_to_profileFragmentOriginal);
movies_into_list.clear();
getAndPostMovieIntoLIstModel.clearPersonalList();
});
toolbar.setonMenuItemClickListener(item -> {
if (item.getItemId() == R.id.menu_edit) {
dialogModifyDescription();
return true;
} else {
return false;
}
});
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu,@NonNull MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_profile_edit_list,menu);
super.onCreateOptionsMenu(menu,inflater);
}
}
fragment_showListContent.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/big_stone"
tools:context=".fragments.MyListsFragment">
<include
layout="@layout/layout_toolbar"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
//other code
</androidx.constraintlayout.widget.ConstraintLayout>
解决方法
您应该有 1 个工具栏和 1 个菜单,而应使用 menu.findItem(R.id.menu_edit).setVisible(true/false);
也许您也可以尝试调用 requireActivity().invalidateOptionsMenu()
。