3

在我的應用程序中有幾個fragment s在activity和我保持backStack這些fragment。一切都好,但其中有一個嵌套的片段。當我把它放入backStack並通過按回來按鈕再次恢復時,片段看起來覆蓋了以前的內容(子片段)。 這是正常的觀點:嵌套片段與後備簡歷

enter image description here

這是重疊視圖的屏幕截圖(當我恢復片段):

enter image description here

你可以得到的差值作爲第二個文本更深(這意味着兒童片段重疊)

我該如何避免這種情況?這對我的分片嵌套代碼:

public class CompetitiveProgramming extends SherlockProgressFragment implements 
     OnChapterSelectListener, OnSubChapterSelectListener { 

    View mContentView; 
    static public List<Chapter> chapterList = new ArrayList<Chapter>(); 
    private ProcessTask processTask = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setRetainInstance(true); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     mContentView = inflater.inflate(
       R.layout.competitive_programming_exercise, container, false); 
     return super.onCreateView(inflater, container, savedInstanceState); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setContentShown(false); 
     setContentView(mContentView); 
     processTask = new ProcessTask(); 
     processTask.execute(); 
    } 


    protected class ProcessTask extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      // background task 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      FragmentTransaction transaction = getChildFragmentManager() 
        .beginTransaction(); 
      if (mContentView.findViewById(R.id.fragment_container) != null) { 
       getChildFragmentManager().beginTransaction() 
         .add(R.id.fragment_container, new ChaptersListFragment()).commit(); 
      } else { 
       transaction.add(R.id.category_fragment, new ChaptersListFragment()); 
       transaction.add(R.id.sub_category_fragment, new SubChaptersListFragment()); 
       transaction.add(R.id.sub_sub_category_fragment, 
         new SubSubChaptersListFragment()); 
      } 
      transaction.commit(); 
      setContentShown(true); 
     } 

    } 

    static protected class Chapter { 
     String chapterTitle; 
     List<SubChapter> subchapterList; 

     public Chapter(String chapterTitle, List<SubChapter> subchapterList) { 
      this.chapterTitle = chapterTitle; 
      this.subchapterList = subchapterList; 
     } 

    } 

    @Override 
    public void onChapterSelected(int position) { 
     SubChaptersListFragment subChaptersListFrag = (SubChaptersListFragment) getChildFragmentManager() 
       .findFragmentById(R.id.sub_category_fragment); 
     if (subChaptersListFrag != null) { 
      subChaptersListFrag.updateList(position); 
     } else { 
      SubChaptersListFragment subChapterFragment = new SubChaptersListFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(SubChaptersListFragment.CHAPTER_POSITION, position); 
      subChapterFragment.setArguments(args); 
      FragmentTransaction transaction = getChildFragmentManager() 
        .beginTransaction(); 
      transaction.replace(R.id.fragment_container, subChapterFragment); 
//   transaction.addToBackStack(null); 
      transaction.commit(); 
     } 
    } 

    @Override 
    public void onSubChapterSelected(int prev, int position) { 
     SubSubChaptersListFragment subSubChaptersListFrag = (SubSubChaptersListFragment) getChildFragmentManager() 
       .findFragmentById(R.id.sub_sub_category_fragment); 
     if (subSubChaptersListFrag != null) { 
      subSubChaptersListFrag.updateList(prev, position); 
     } else { 
      SubSubChaptersListFragment subSubChapterFragment = new SubSubChaptersListFragment(); 
      Bundle args = new Bundle(); 
      args.putIntArray(SubSubChaptersListFragment.POSITIONS, new int[]{prev, position}); 
      subSubChapterFragment.setArguments(args); 
      FragmentTransaction transaction = getChildFragmentManager() 
        .beginTransaction(); 
      transaction.replace(R.id.fragment_container, subSubChapterFragment); 
//   transaction.addToBackStack(null); 
      transaction.commit();   
     } 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     if (processTask != null && processTask.getStatus() != AsyncTask.Status.FINISHED) { 
      processTask.cancel(true); 
     } 
    } 

} 
+2

猜想它可能與transaction.add()有關,嘗試使用替換,只要你可以 –

+0

我用'替換'無處不在:( –

回答

2

基里爾庫拉科夫是正確的。應該使用replace而不是add。我編輯的代碼:

public class CompetitiveProgramming extends SherlockProgressFragment implements 
     OnChapterSelectListener, OnSubChapterSelectListener { 

    View mContentView; 
    static public List<Chapter> chapterList = new ArrayList<Chapter>(); 
    private ProcessTask processTask = null; 
    Fragment chapterFragment = null; 
    Fragment subChapterFragment = null; 
    Fragment subSubChapterFragment = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setRetainInstance(true); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     mContentView = inflater.inflate(
       R.layout.competitive_programming_exercise, container, false); 
     return super.onCreateView(inflater, container, savedInstanceState); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setContentShown(false); 
     setContentView(mContentView); 
     processTask = new ProcessTask(); 
     processTask.execute(); 
    } 


    protected class ProcessTask extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      // background task 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      FragmentTransaction transaction = getChildFragmentManager() 
      .beginTransaction(); 
      chapterFragment = new ChaptersListFragment(); 
      if (mContentView.findViewById(R.id.fragment_container) != null) { 
       transaction.replace(R.id.fragment_container, chapterFragment); 
      } else { 
       subChapterFragment = new SubChaptersListFragment(); 
       subSubChapterFragment = new SubSubChaptersListFragment(); 
       transaction.replace(R.id.category_fragment, chapterFragment); 
       transaction.replace(R.id.sub_category_fragment, subChapterFragment); 
       transaction.replace(R.id.sub_sub_category_fragment, subSubChapterFragment); 
      } 
      transaction.commit(); 
      setContentShown(true); 
     } 

    } 

    static protected class Chapter { 
     String chapterTitle; 
     List<SubChapter> subchapterList; 

     public Chapter(String chapterTitle, List<SubChapter> subchapterList) { 
      this.chapterTitle = chapterTitle; 
      this.subchapterList = subchapterList; 
     } 

    } 

    @Override 
    public void onChapterSelected(int position) { 
     SubChaptersListFragment subChaptersListFrag = (SubChaptersListFragment) getChildFragmentManager() 
       .findFragmentById(R.id.sub_category_fragment); 
     if (subChaptersListFrag != null) { 
      subChaptersListFrag.updateList(position); 
     } else { 
      SubChaptersListFragment subChapterFragment = new SubChaptersListFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(SubChaptersListFragment.CHAPTER_POSITION, position); 
      subChapterFragment.setArguments(args); 
      FragmentTransaction transaction = getChildFragmentManager() 
        .beginTransaction(); 
      transaction.replace(R.id.fragment_container, subChapterFragment); 
//   transaction.addToBackStack(null); 
      transaction.commit(); 
     } 
    } 

    @Override 
    public void onSubChapterSelected(int prev, int position) { 
     SubSubChaptersListFragment subSubChaptersListFrag = (SubSubChaptersListFragment) getChildFragmentManager() 
       .findFragmentById(R.id.sub_sub_category_fragment); 
     if (subSubChaptersListFrag != null) { 
      subSubChaptersListFrag.updateList(prev, position); 
     } else { 
      SubSubChaptersListFragment subSubChapterFragment = new SubSubChaptersListFragment(); 
      Bundle args = new Bundle(); 
      args.putIntArray(SubSubChaptersListFragment.POSITIONS, new int[]{prev, position}); 
      subSubChapterFragment.setArguments(args); 
      FragmentTransaction transaction = getChildFragmentManager() 
        .beginTransaction(); 
      transaction.replace(R.id.fragment_container, subSubChapterFragment); 
//   transaction.addToBackStack(null); 
      transaction.commit();   
     } 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     if (processTask != null && processTask.getStatus() != AsyncTask.Status.FINISHED) { 
      processTask.cancel(true); 
     } 
    } 

} 

希望這會有所幫助!

+1

你的ProcessTask在後臺沒有做任何事情,它在一個AsyncTask中 – Nickmccomb

0

你可能添加的片段在你onResume(),這是不是你想要的,因爲每個你重新加入新片段的時間是什麼。只需修復您的代碼邏輯

+0

你好,先生,謝謝你的回答!這是我問的問題和A-- C給了我一個很好的解決方案,在backStack中添加片段,我跟着它,請看看,並給我一些示例代碼,你已經提出了一些建議:) –