我有一个带有片段容器的 Activity 和两个片段,如“列表片段 - > 详细片段”
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
,所以第二个片段永远不会刷新。已经尝试删除、分离,但没有任何效果:第一个片段始终显示为已添加且可见。还在平板电脑和手机上测试它,同样的事情也会发生。也许我遗漏了一些东西,但无法正确处理,因此我们将不胜感激。谢谢