首页 > 解决方案 > 重新加载片段

问题描述

大家,我的问题是:

我在 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" />

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

标签: androidandroid-fragmentsreload

解决方案


运行这个

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

推荐阅读