2015-01-09 65 views
0

那麼,考慮一個片段有兩個子片段的情況,例如在主片段xml中聲明兩個片段時,如何銷燬片段?將所有三個片段添加到後端堆棧嗎?銷燬處於分層關係的片段

主要考慮片段A和考慮它的類文件..

public class fragmentA extends Fragment { 
public static FragmentC fragmentC; 
private static View view; 

@Override 
public View onCreateView(LayoutInflater inflater, 
     @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

    // TODO Auto-generated method stub 
    if(view!=null){ 
      ViewGroup parent = (ViewGroup) view.getParent(); 
      if (parent != null) 
       parent.removeView(view); 
    } 
    try{ 
     view = inflater.inflate(R.layout.fragment_a, container, 
       false); 

     fragmentC= (fragmentC) getActivity() 
       .getSupportFragmentManager().findFragmentById(
         R.id.fragment_c); 
    }catch(InflateException e){ 
     e.printStackTrace(); 
    } 
return view; 

} 

現在考慮片段的XML文件fragment_a

<LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 

       <fragment 
        android:id="@+id/FragmentB" 
        android:name="com.example.FragmentB" 
        android:layout_width="320dp" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        tools:layout="@layout/fragment_b" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 

       <fragment 
        android:id="@+id/FragmentC" 
        android:name="com.example.FragmentC" 
        android:layout_width="300dp" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:layout_marginBottom="5dp" 
        android:layout_marginTop="5dp" 
        android:visibility="visible" 
        tools:layout="@layout/fragment_c" /> 
      </LinearLayout> 
在上述情況下

,如何摧毀的片段?我們是否必須單獨銷燬所有三個碎片,或者我們是否必須摧毀碎片a,其餘的碎片會自毀?同時所有的工作都在碎片B做

回答

1

答案很簡單..

所有的u必須做的,你從頁面 檢查下面的代碼 移動添加該代碼摧毀每一個片段到兩個頁面.. pdmf是一個靜態參考烏爾類(片段A或片段C) 然後調用兩個片段此代碼當u從片段B,移動

public static void removeFragment() { 
    try { 
     FragmentTransaction ft = ((FragmentActivity) activityContext).getSupportFragmentManager() 
       .beginTransaction(); 
     ft.remove((Fragment)pdMF); 

     ft.commit(); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
}