3 回答
TA贡献1789条经验 获得超10个赞
片段onResume()
或onPause()
仅在调用活动onResume()
或时才onPause()
被调用。他们紧密地联系在一起Activity
。
TA贡献1847条经验 获得超11个赞
这是我的Gor答案的更可靠的版本(使用fragments.size()是不可靠的,因为在弹出片段后大小不会减小)
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
if (getFragmentManager() != null) {
Fragment topFrag = NavigationHelper.getCurrentTopFragment(getFragmentManager());
if (topFrag != null) {
if (topFrag instanceof YourFragment) {
//This fragment is being shown.
} else {
//Navigating away from this fragment.
}
}
}
}
});
和“ getCurrentTopFragment”方法:
public static Fragment getCurrentTopFragment(FragmentManager fm) {
int stackCount = fm.getBackStackEntryCount();
if (stackCount > 0) {
FragmentManager.BackStackEntry backEntry = fm.getBackStackEntryAt(stackCount-1);
return fm.findFragmentByTag(backEntry.getName());
} else {
List<Fragment> fragments = fm.getFragments();
if (fragments != null && fragments.size()>0) {
for (Fragment f: fragments) {
if (f != null && !f.isHidden()) {
return f;
}
}
}
}
return null;
}
- 3 回答
- 0 关注
- 754 浏览
添加回答
举报