Reputation: 186
i use three fragment in dashbordActivity on bottom navigation bar. On back button press previous fragment view properly but bottom navigation button not changing.
in first image home fragment is already selected after i select profile from bottom navigation and press backbutton. fragment changes but bottom navigation item does not change.
this is my bottom navigation view .....
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
setTitle("Home");
fragment = new FirstFragment();
loadFragment(fragment);
return true;
case R.id.navigation_favorite:
setTitle("favourite");
fragment = new MyFavoriteAdsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
setTitle("Profile");
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
case R.id.navigation_myads:
setTitle("Chat");
fragment = new MyAdsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_chat:
// mTextMessage.setText(R.string.title_chat);
setTitle("Chat");
fragment = new TabFragmentChatUserList();
loadFragment(fragment);
/* Intent i=new Intent(getApplicationContext(),Chat.class);
startActivity(i);*/
return true;
}
return false;
}
};
private void loadFragment(Fragment fragment) {
// load fragment
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack("TAG");
transaction.commit();
/* fragmentManager.beginTransaction().replace(R.id.home_container, frag).addToBackStack(null).commit();*/
}
ob back press i tryed...
@Override
public void onBackPressed()
{
// if your using fragment then you can do this way
int fragments
=getSupportFragmentManager().getBackStackEntryCount();
if (fragments == 1) {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", null)
.show();
} else {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
if(navigation.getSelectedItemId () != R.id.navigation_favorite)
{
navigation.setSelectedItemId(R.id.navigation_favorite);
}
else if(navigation.getSelectedItemId () != R.id.navigation_chat)
{
navigation.setSelectedItemId(R.id.navigation_chat);
}
else if(navigation.getSelectedItemId () != R.id.navigation_profile)
{
navigation.setSelectedItemId(R.id.navigation_profile);
}
else
{
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
Upvotes: 2
Views: 4822
Reputation: 312
You can customize your onBackPressed
in this way:
@Override
public void onBackPressed() {
// if your using fragment then you can do this way
int fragments
= getSupportFragmentManager().getBackStackEntryCount();
if (fragments == 1) {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", null)
.show();
} else {
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
Fragment selectedFragment = null;
List<Fragment> fragments = getSupportFragmentManager.getFragments();
for (Fragment fragment : fragments) {
if (fragment != null && fragment.isVisible()) {
selectedFragment = fragment;
break;
}
}
if (selectedFragment instanceof yourFirstFragment) {
navigation.setSelectedItemId(R.id.your_first_item);
} if (selectedFragment instanceof yourSecondFragment) {
navigation.setSelectedItemId(R.id.your_second_item);
} if (selectedFragment instanceof yourThirdFragment) {
navigation.setSelectedItemId(R.id.your_third_item);
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
Upvotes: 0
Reputation: 8853
You need to check your backstack and add listener to your fragment manager, after that based on your fragment you need to set particular button check based on your fragment.
fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
// If the stack decreases it means I clicked the back button
if( fragmentManager.getBackStackEntryCount() <= count){
//check your position based on selected fragment and set it accordingly.
navigation.getMenu().getItem(your_pos).setChecked(true);
}
}
});
Upvotes: 4