2013-11-21 32 views
0

它中的片段是否接收onDetach,onDestroy方法?或者我需要先刪除片段,然後清除視圖?如果我清除了片段的容器視圖將會發生什麼

+0

什麼?我根本不明白它 – pskink

+0

我的意思是,如果一個片段被附加在viewcontainer下,如果我調用「viewContainer.removeAllViews()」,那麼片段中會發生什麼,該片段是否會收到「onDetach」,「onDestroy」方法。 – virsir

回答

0

根據我的理解,它的順序並不重要。如果清除視圖,則片段的元素將從活動視圖中刪除,但仍需要將其從片段管理器中移除。

例如,我創造的東西就像我的活動如下:

ViewGroup viewFragments = (ViewGroup)findViewById(R.id.layout_fragments); 
viewFragments.removeAllViews(); 

View child = ViewGroup.inflate(mActivity, R.layout.child_edit_item, null); 
viewFragments.addView(child); 

Fragment f = new MyFragment(); 

FragmentManager mgr = mActivity.getSupportFragmentManager(); 
FragmentTransaction trans = mgr.beginTransaction(); 
if (mgr.findFragmentByTag(p.getKey()) != null) { 
    trans.replace(R.id.fragment_container, f, p.getKey()); 
} else { 
    trans.add(R.id.fragment_container, f, p.getKey()); 
} 
trans.commit(); 
相關問題