Este es mi código para el BottomNavigationView, quisiera saber como hacer para al presionar back regrese al primer fragment y no se cierre la aplicación.
public class Mapa extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, new MapFragment()).commit();
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomViewBar);
bottomNavigationViewEx.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
}
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment()).commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment()).commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment()).commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment()).commit();
return true;
}
return false;
}
};
}