我有一个带有片段容器和两个片段的活动,类似于“列表片段 -> 详细信息片段”。
private boolean isFirstFragmentAdded() {
return getSupportFragmentManager().findFragmentByTag("FIRST_FRAGMENT_TAG") != null;
}
private boolean isFirstFragmentVisible() {
return getSupportFragmentManager().findFragmentByTag("FIRST_FRAGMENT_TAG").isVisible();
}
private boolean isSecondFragmentAdded() {
return getSupportFragmentManager().findFragmentByTag("SECOND_FRAGMENT_TAG") != null;
}
private boolean isSecondFragmentVisible() {
return getSupportFragmentManager().findFragmentByTag("SECOND_FRAGMENT_TAG").isVisible();
}
private void addFirstFragment() {
firstFragment = new FirstFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment, "FIRST_FRAGMENT_TAG").commit();
}
private void showFirstFragment() {
firstFragment = new FirstFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, firstFragment, "FIRST_FRAGMENT_TAG").commit();
}
private void addSecondFragment() {
secondFragment = new SecondFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, secondFragment, "SECOND_FRAGMENT_TAG").commit();
}
private void showSecondFragment() {
secondFragment = new SecondFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, secondFragment, "SECOND_FRAGMENT_TAG").commit();
}
private void refreshVisibleFragment() {
if (isFirstFragmentAdded() && isFirstFragmentVisible()) {
refreshFirstFragment();
} else if (isSecondFragmentAdded() && isSecondFragmentVisible()) {
refreshSecondFragment();
}
}
问题是即使我的第二个片段可见,第一个片段也总是返回isVisible() == true
,因此第二个片段永远不会刷新。已经尝试过移除、分离,但都没有效果:第一个片段总是以添加和可见状态出现。在平板电脑和手机上进行了测试,情况也是一样的。也许我漏掉了什么,但无法正确解决,所以任何帮助将不胜感激。谢谢。