如何解决为什么 replace() 方法保存 Fragment 的状态?
我有一个 BottomNavigationView
可以在 4 Fragments
之间导航,第一个有 RecyclerView
。当我选择 BottomNavigationView
的一个项目时,我使用 replace()
方法在 Fragments
之间切换。我不明白的是为什么当我滚动 RecyclerView
时,转到另一个 Fragment
并返回带有 Fragment
的 RecyclerView
,滚动位置保持与我离开 Fragment
时相同,而不是回到 RecyclerView
的顶部。由于我使用的是 replace()
并且没有添加到后台堆栈中,因此片段不应该被重置吗?这是我的代码:
private void initializefragments() {
homeFragment = new HomeFragment();
FeedFragment = new FeedFragment();
chatFragment = new ChatFragment();
profileFragment = new ProfileFragment();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.fragment_container,homeFragment,"home")
.commit();
}
private void setupBottomNavigation() {
bottomNavigationView.setonNavigationItemSelectedListener(item -> {
FragmentTransaction ft = fragmentManager.beginTransaction();
if (item.getItemId() == R.id.bottom_nav_home) {
ft.replace(R.id.fragment_container,"home")
.commit();
} else if (item.getItemId() == R.id.bottom_nav_Feed) {
ft.replace(R.id.fragment_container,FeedFragment,"Feed")
.commit();
} else if (item.getItemId() == R.id.bottom_nav_chat) {
ft.replace(R.id.fragment_container,chatFragment,"chat")
.commit();
} else if (item.getItemId() == R.id.bottom_nav_profile) {
ft.replace(R.id.fragment_container,profileFragment,"profile")
.commit();
}
return true;
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。