Android: how to load fragment into FrameLayout
First you have a mistake in your Fragment transaction line, according with your layout should be:
transaction.replace(R.id.contentContainer, newFragment); // not R.id.bottomBar
Second, you should use supportFragmentManager instead of fragmentManager to work with support fragments, so implement the following way:
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentContainer, newFragment);
transaction.addToBackStack(null);
transaction.commit();
in my case i solve it by using
androidx.fragment.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
replace your
FragmentTransaction
bottomNavigationView= findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@SuppressLint("NonConstantResourceId")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment temp =null;
switch (item.getItemId()) {
case R.id.library:
temp = new LibraryFragment();
break;
case R.id.search:
temp = new SearchFragment();
break;
case R.id.profile:
temp = new UserprofileFragment();
break;
default:
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, temp).commit();
return true;
}
});
getSupportFragmentManager().beginTransaction().replace(R.id.fragment,new SearchFragment()).commit();