重新加载时刷新片段 [英] Refresh Fragment at reload
问题描述
在一个 android 应用程序中,我将数据从 Db 加载到 Fragment
内的 TableView
.但是当我重新加载 Fragment
时,它会显示以前的数据.我可以用当前数据而不是以前的数据重新填充 Fragment
吗?
In an android application I'm loading data from a Db into a TableView
inside a Fragment
. But when I reload the Fragment
it displays the previous data. Can I repopulate the Fragment
with current data instead of previous data?
推荐答案
我想你想在数据库更新时刷新片段内容
如果是这样,分离片段并重新附加
If so, detach the fragment and reattach it
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Your_Fragment_TAG 是您在创建片段时为其指定的名称
Your_Fragment_TAG is the name you gave your fragment when you created it
此代码用于支持库.
如果您不支持旧设备,只需使用 getFragmentManager 而不是 getSupportFragmentManager
If you're not supporting older devices, just use getFragmentManager instead of getSupportFragmentManager
此方法要求 Fragment 有标签.
如果您没有它,那么@Hammer 的方法就是您所需要的.
This method requires the Fragment to have a tag.
In case you don't have it, then @Hammer's method is what you need.
这篇关于重新加载时刷新片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!