我有一个连接到一个类的片段。我有一个后台线程,它每 1 分钟在后台工作一次,如果用户正在“观看”屏幕,我需要专门刷新 UI。
UI 有一些 TextViews 等。我通过这样的处理程序更新 UI:
// Updating TextViews and such with textView.SetText(); ....
if (isResumed()) {
try {
// This throws an error sometimes saying: getFragmentManager() can't be called on null instance
getFragmentManager().beginTransaction().replace(R.id.fragment_container, new LiveViewFragment()).commitNow();
} catch (Exception e) {
e.printStackTrace();
}
}
我的问题是: 如何强制刷新片段?
我一直在尝试的其他代码是这样的:
LiveViewFragment fragment = (LiveViewFragment) getFragmentManager().findFragmentById(R.id.fragment_container);
getFragmentManager().beginTransaction().detach(fragment).attach(fragment).commit();
但这有时也会失败。