2013-08-28 130 views
6

我有一個兩個窗格佈局,左邊是我的按鈕。當按下按鈕時,其相應的片段顯示在右側。有時片段重疊,但這是一個間歇性問題。我無法一直複製它,但它確實發生了片段重疊有時切換標籤

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.imageButtonSettingsManageBooks: 
     SettingsManageBooksFragment mbFragment = new SettingsManageBooksFragment(); 
     getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, mbFragment).addToBackStack(null).commit(); 
     mImgFragmentTitle.setImageResource(R.drawable.manage_my_books); 
     this.getSupportFragmentManager().executePendingTransactions(); 


     break; 
    case R.id.imageButtonSettingsPurchaseHistory: 
     SettingsPurchaseHistoryFragment phFragment = new SettingsPurchaseHistoryFragment(); 
     getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, phFragment).commit(); 
     mImgFragmentTitle.setImageResource(R.drawable.purchase_history); 
     this.getSupportFragmentManager().executePendingTransactions(); 

     break; 
    case R.id.imageButtonSettingsAudio: 
     SettingsAudioFragment aFragment = new SettingsAudioFragment(); 
     getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, aFragment).commit(); 
     mImgFragmentTitle.setImageResource(R.drawable.audio); 
     this.getSupportFragmentManager().executePendingTransactions(); 

     break; 
    case R.id.imageButtonSettingsRestore: 
     SettingsRestoreFragment rFragment = new SettingsRestoreFragment(); 
     getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, rFragment).commit(); 
     mImgFragmentTitle.setImageResource(R.drawable.restore); 
     this.getSupportFragmentManager().executePendingTransactions(); 

     break; 
    case R.id.imageButtonSettingsCopyright: 
     SettingsCopyrightFragment cFragment = new SettingsCopyrightFragment(); 
     getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, cFragment).commit(); 
     mImgFragmentTitle.setImageResource(R.drawable.copyright); 
     this.getSupportFragmentManager().executePendingTransactions(); 

     break; 
    case R.id.imageButtonSettingsAbout: 
     SettingsAboutFragment abFragment = new SettingsAboutFragment(); 
     getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, abFragment).commit(); 
     mImgFragmentTitle.setImageResource(R.drawable.about); 
     this.getSupportFragmentManager().executePendingTransactions(); 

     break; 

看起來像是因爲我用一個可能導致重疊的新片段替換每個片段。在提交新片段之前是否有辦法清除所有片段?

+0

我得到同樣的問題,(總是使用替換以及),但我使用支持庫。編輯:看起來你也是,但你調用getFragmentManager而不是getSupportFragmentManager ...可能是你的問題?和我一樣,這是一個非常間歇性的問題,我們無法複製。 – StackOverflowed

+0

它不會讓我編輯我以前的評論,但編輯2:如果它很重要,我們看到它發生的一個設備是三星Galaxy S4。 – StackOverflowed

+1

我認爲我們有另一種解決方案比通過這種方式去除解決方案更好。 在'所有的xml文件'中,應該爲它定義背景色,它將解決問題: 將這個'android:background =「@ android:color/black」'添加到您定義的視圖標記中。 –

回答

2

正如上面提到的StackOverflowed,這是一個間歇性問題。我發現了一種解決方法,通過編寫自己的代碼在提交新代碼之前刪除任何現有片段。下面是我的代碼:

 fragmentManager = getFragmentManager(); 
     ft = fragmentManager.beginTransaction(); 

     mbFragment = new SettingsManageBooksFragment(); 
     ft.replace(R.id.setting_detail_container2, mbFragment).commit(); 
     mImgFragmentTitle.setImageResource(R.drawable.manage_my_books); 

    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.imageButtonSettingsManageBooks: 

      if (mPurchaseHistory == true) { 
       ft.remove(phFragment); 

       Log.d(TAG, "REMOVING PURCHASE HISTORY FRAG"); 

      } else if (mAudio == true) { 
       ft.remove(aFragment); 

       Log.d(TAG, "REMOVING AUDIO FRAG"); 
      } else if (mRestore == true) { 
       ft.remove(rFragment); 

       Log.d(TAG, "REMOVING RESTORE FRAG"); 
      } else if (mCopyright == true) { 
       ft.remove(cFragment); 

       Log.d(TAG, "REMOVING COPYRIGHT FRAG"); 
      } else if (mAbout == true) { 
       ft.remove(abFragment); 

       Log.d(TAG, "REMOVING ABOUT FRAG"); 
      } 
      ft = fragmentManager.beginTransaction(); 
      mbFragment = new SettingsManageBooksFragment(); 
      ft.replace(R.id.setting_detail_container2, mbFragment).commit(); 
      mImgFragmentTitle.setImageResource(R.drawable.manage_my_books); 
      mManageBooks = true; 
      mPurchaseHistory = false; 
      mAudio = false; 
      mRestore = false; 
      mCopyright = false; 
      mAbout = false; 

      break;