当使用bottomNavigationView在片段之间切换时,每次按下按钮时都会重新创建片段。
这是我的代码:
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentguest, fragment)
.commit();
return true;
}
return false;
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.eventsguest:
fragment = new Events();
break;
case R.id.about_usguest:
fragment = new About_Us();
break;
}
return loadFragment(fragment);
}