Lograr que mi BottomNavigationView pudiera cambiar entre fragments como yo quería, fue algo complicado. Te muestro primero el código de mi ContainerActivity funcional, quizás te sirve para solventar:
publicclassContainerActivityextendsAppCompatActivity{
privateboolean viewIsAtHome;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottombar);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem menuItem){
int itemId = menuItem.getItemId();
switch (itemId){
case R.id.home:
addFragment(new HomeFragment());
viewIsAtHome = true;
break;
case R.id.profile:
addFragment(new ProfileFragment());
viewIsAtHome = false;
break;
case R.id.search:
addFragment(new SearchFragment());
viewIsAtHome = false;
break;
}
returntrue;
}
});
bottomNavigationView.setSelectedItemId(R.id.home);
}
privatevoidaddFragment(Fragment fragment){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null)
.commit();
}
@OverridepublicvoidonBackPressed(){
if (!viewIsAtHome) { //if the current view is not the News fragment
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottombar);
bottomNavigationView.setSelectedItemId(R.id.home); //display the News fragment
} else {
moveTaskToBack(true); //If view is in News fragment, exit application
}
}
}
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottombar);
privatevoidaddFragment(Fragment fragment){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null)
.commit();
}
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem menuItem){
int itemId = menuItem.getItemId();
switch (itemId){
case R.id.home:
addFragment(new HomeFragment());
break;
case R.id.profile:
addFragment(new ProfileFragment());
break;
case R.id.search:
addFragment(new SearchFragment());
break;
}
returntrue;
}
});
bottomNavigationView.setSelectedItemId(R.id.home);
Si quieres hacer que el botón back te mande a fragment home, puedes hacer lo siguiente:
privateboolean viewIsAtHome;
viewIsAtHome = false;
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@OverridepublicbooleanonNavigationItemSelected(@NonNull MenuItem menuItem){
int itemId = menuItem.getItemId();
switch (itemId){
case R.id.home:
addFragment(new HomeFragment());
viewIsAtHome = true;
break;
case R.id.profile:
addFragment(new ProfileFragment());
viewIsAtHome = false;
break;
case R.id.search:
addFragment(new SearchFragment());
viewIsAtHome = false;
break;
}
returntrue;
}
});
@Override
public void onBackPressed() {
if (!viewIsAtHome) { //Si la vista actual no es el fragment Home
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottombar);
bottomNavigationView.setSelectedItemId(R.id.home); //Selecciona el fragment Home
} else {
moveTaskToBack(true); //Si presionas Back cuando ya muestras el fragment Home, sale de la app
}
}
¿Qué es el fragment_container?
How to open the save password and Microsoft edge if you are know the right information how to open saved passwords in microsoft edge then you are require the help of this website content and you are read the more information.
Buenas, si el HomeFragment es mi MainActivity como lo hago para volver de un fragment al MAIN? gracias