我有Base Activity
包括NavigationView
和menu items
。在开始时,它会加载Home fragment
,其中包含background image
。每个加载特定fragment
。当我选择条款&条件菜单项,它加载T& C fragment
&当我按下后退按钮时它就会杀掉它。
但是,当我选择“关于我们”菜单项时,它会加载关于我们fragment
,但我需要按BACK button
两次才能将其删除。我需要知道它为什么会发生?
AppBaseActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
HomeFragment homeFragment = new HomeFragment();
fragmentTransaction.add(R.id.body_container, homeFragment, "");
fragmentTransaction.commit();
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
navigationView.getMenu().findItem(item.getItemId()).setChecked(true);
switch (item.getItemId()) {
case R.id.nav_terms :
fragmentTransaction = fragmentManager.beginTransaction();
TCFragment tcFragment = new TCFragment();
fragmentTransaction.replace(R.id.body_container, tcFragment, "");
fragmentTransaction.commit();
break;
case R.id.nav_about_us :
fragmentTransaction = fragmentManager.beginTransaction();
AboutUsFragment aboutUsFragment = new AboutUsFragment();
fragmentTransaction.replace(R.id.body_container, aboutUsFragment, "");
fragmentTransaction.commit();
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
All fragments
只是通过夸大受尊敬的xml来覆盖onCreateView()
。尚未在fragments
中编写代码。
答案 0 :(得分:0)
如果需要,可以停止硬件导航。
只需使用onBackPressed()
而不使用super.onBackPressed()
@Override
public void onBackPressed() {
}
答案 1 :(得分:-2)
@Override
public void onBackPressed() {
super.onBackPressed();
}