android-fragments 片段到片段

hfwmuf9z  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(296)

我想将片段传递给片段,但如果我传递新片段,我会显示旧片段部件,为什么?

这段代码在MessageFragment中我想从MessageFragment转到HomeFragment,但我可以,但我显示消息片段edittext、imageview等。为什么?

public void goMessagesToHome(){
    HomeFragment homeFragment = new HomeFragment();
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragmentMessage, homeFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}
dy2hfwbg

dy2hfwbg1#

已解决
我必须这样做,我有navhostfragment所以我写的.

public void goMessagesToHome(){
    HomeFragment homeFragment = new HomeFragment();
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.navHostFragment, homeFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

(R.id.navHostFragment,家庭片段);
这个地方很重要

相关问题