如何修复“ NavHostFragment的后堆栈上无效的后堆栈条目”?

时间:2019-11-01 06:14:45

标签: android

  

错误:NavHostFragment的后堆栈上的无效后堆栈条目-如果需要从通过导航图创建的片段中进行自定义FragmentTransactions,请使用getChildFragmentManager()。

     

这是我的片段主页:

gallery= root.findViewById(R.id.gallery);

    gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          //  Toast.makeText(getActivity(), "GAL", Toast.LENGTH_SHORT).show();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.nav_host_fragment, new GalleryFragment(), "GalleryFragmentTag");
            getActivity().setTitle("your title");
            ft.commit();
            ft.addToBackStack(null);
        }
    });
    return root;
  

这是nav_host_fragment:

   <fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />
  

这是GalleryFragment.java:

onCreateView(@NonNull LayoutInflater充气机的公共视图,                              ViewGroup容器,捆绑了saveInstanceState){

    View root = inflater.inflate(R.layout.fragment_gallery, container, false);
    gallery_recycler_view= root.findViewById(R.id.gallery_recycler_view);

    final ProgressDialog pd = new ProgressDialog(getActivity());
    pd.setMessage("Loading");
    pd.show();

    gallery_recycler_view.setHasFixedSize(true);
    final GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2, RecyclerView.VERTICAL, false);
    gallery_recycler_view.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
    galleryModelList = new ArrayList<>();

    DatabaseReference dbProducts = FirebaseDatabase.getInstance().getReference("OImages");
    Log.e("path1",dbProducts.toString());

    dbProducts.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {


                for (DataSnapshot productSnapshot : dataSnapshot.getChildren()) {
                    GalleryModel p = productSnapshot.getValue(GalleryModel.class);
                    galleryModelList.add(p);
                }

                adapter = new GalleryAdapter(getActivity(), galleryModelList);


                Collections.reverse(galleryModelList);
                gallery_recycler_view.setAdapter(adapter);

               // lovelyProgressDialog.dismiss();
                pd.dismiss();

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    return root;
}

0 个答案:

没有答案