这是我的底部导航视图.....
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();*/
}
我尝试过后退...
@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();
}
}
答案 0 :(得分:2)
您需要检查堆栈,并将侦听器添加到片段管理器中,然后根据片段,您需要根据片段设置特定的按钮检查。
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);
}
}
});
答案 1 :(得分:0)
您可以通过以下方式自定义onBackPressed
:
@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();
}
}