@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen_bottombar);
BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
initBottomBar(bottomNavigationView);}
private void initBottomBar(BottomNavigationView bottomNavigationView) {
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
fragmentTag = "home";
savedFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (savedFragment == null)
savedFragment = NeedyFeedyHomeTabFragment.getInstance();
break;
case R.id.search:
fragmentTag = "search";
savedFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (savedFragment == null) {
savedFragment = SearchResultFragment.getInstance();
}
break;
case R.id.profile:
fragmentTag = "profile";
savedFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (savedFragment == null) {
savedFragment = FragmentProfileDetails.getInstance();
}
break;
case R.id.favourite:
fragmentTag = "favourite";
savedFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);
if (savedFragment == null) {
savedFragment = FavouriteListFragment.getInstance();
}
break;
}
fragmentTransactionExecution(savedFragment, fragmentTag);
return true;
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, NeedyFeedyHomeTabFragment.getInstance(), "home");
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
}
private void fragmentTransactionExecution(Fragment selectedFragment, String fragmentTag) {
if (selectedFragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment, fragmentTag);
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
}
}
答案 0 :(得分:-1)
在view.setTag(this);
中的每个片段中添加onCreateView
。