重新加载片段

时间:2020-02-27 12:34:58

标签: android android-fragments reload

每个人,我的问题是:

我在ProfilFragment中,它是由Menu Drawer生成的,它扩展了Fragment,但是我无法更改它(它破坏了其他部分)。 当我点击一个按钮时,我想刷新当前的片段

// Reload current fragment
                        Fragment frg = null;
                        frg = getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_profil);
                        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                        ft.detach(frg);
                        ft.attach(frg);
                        ft.commit();

我在其他StackOverflow问题上发现了此问题,但对我而言不起作用。 frg = getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_profil); 始终返回null

即使在我的mobile_navigation.xml中 我有

<fragment
    android:id="@+id/nav_profil"
    android:name="com.example.pierregignoux.ui.profil.ProfilFragment"
    tools:layout="@layout/fragment_profil" />

任何帮助都会很棒,非常感谢。

1 个答案:

答案 0 :(得分:0)

只需运行

getActivity().getSupportFragmentManager().beginTransaction().detach(this).attach(this).commit();