android - 重新加载片段
问题描述
大家,我的问题是:
我在 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" />
任何帮助都会很棒,非常感谢。
解决方案
运行这个
getActivity().getSupportFragmentManager().beginTransaction().detach(this).attach(this).commit();
推荐阅读
- python - 如何让 pylint 识别模拟的方法成员?
- javascript - 模态框关闭后如何在屏幕上显示两个图像?
- docker - 如何取消设置在运行时设置的 ENV 变量
- javascript - fabric.js:如何为图像创建边界框?
- mysql - 如何在mysql中找到具有特定条件的特定月份?
- ios - VStack 的帧大小没有填满整个屏幕
- reactjs - 条件渲染功能组件具有多案例和复杂的逻辑
- php - 使用 Homestead 连接和迁移数据库的问题
- python - 如何修复尝试迁移 manage.py 的错误
- node.js - 将无效数据传递给模式时,猫鼬不会引发错误