我在BackStack中添加片段。并从Fragment显示/隐藏MainActivity的后箭头。
Phone的BackButton软键正在按预期工作。但ActionBar上的后退箭头退出了应用程序。它甚至不会触发onBackPressed或onKeyDown。任何人都能告诉我我错过了什么吗?
以下是我的MainActivity的代码。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BoxItemFragment fragment = new BoxItemFragment();
fragmentTransaction.replace(R.id.fragContainer, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onBackPressed(){ //Not Firing
if(getFragmentManager().getBackStackEntryCount() > 0){
getFragmentManager().popBackStack();
}
else{
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){ //Not Firing
if(keyCode == KeyEvent.KEYCODE_BACK){
if(getFragmentManager().getBackStackEntryCount() > 0){
getFragmentManager().popBackStack();
}
else{
// super.onBackPressed();
}
}
return true;
}
}