我有Base Activity
包括NavigationView
與2 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
中沒有編寫代碼。
使用。新增的,而不是.replace裝載片段,看看 – Redman
你覆蓋'onBackPressed()' –
但是爲什麼它那麼T&C片段的情況下工作? – VVB