gpt4 book ai didi

android - 加载另一个 fragment 时, fragment 数据变为空

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:13 26 4
gpt4 key购买 nike

NavigationDrawer 中,我使用了两个 fragment 。在每个 fragment 中,我都有选项卡 slider 。当我第一次点击时,两个 fragment 中的数据都出现了,但是当我从一个 fragment 转到另一个 fragment 时,两个 fragment 中的数据都是空的。我已经检查了 fragment 的保存状态,但问题仍然没有解决。知道我们如何解决这个问题吗?

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.frame, new LoginFragment()).commit();
}

private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
System.out.println("Navigation Drawer Item Clciked ---->" + navDrawerClickedItem);
Fragment fragment = null;
switch (menuItem.getItemId()) {
case R.id.roof:
if (navDrawerClickedItem == menuItem.getItemId()) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
System.out.println("New roof Fragment already loaded");
} else {
navDrawerClickedItem = menuItem.getItemId();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new RoofTabFragment()).commit();
System.out.println("New roof Fragment Created");
}
break;
case R.id.floor:
if (navDrawerClickedItem == menuItem.getItemId()) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
System.out.println("Floor Fragment already loaded");
} else {
navDrawerClickedItem = menuItem.getItemId();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, new FloorTabFragment()).commit();
System.out.println("Floor Fragment Created");
}
break;
case R.id.signOut:
LogMeOut();
break;
}
if (fragment != null) {
mFragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
} else {
Log.e("MainActivity", "Error in creating fragment");
}
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawerLayout.closeDrawers();
return false;
}
});
}

最佳答案

通过将 viewPager.setAdapter(new MyAdapter(getChildFragmentManager())); 中的 getFragmentManager() 替换为 getChildFragmentManager() 解决了这个问题。

关于android - 加载另一个 fragment 时, fragment 数据变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32500972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com