2013-08-05 137 views
1

我有一個活動,我想根據設備的方向更改片段。所以這裏是我的代碼:完全破壞片段Android

FragmentTransaction ft = getFragmentManager().beginTransaction(); 

if(getFragmentManager().findFragmentById(R.id.calendrier_calendrierhebdomadaire) != null){ 
     Fragment fragment = getFragmentManager().findFragmentById(R.id.calendrier_calendrierhebdomadaire); 
     ft.remove(fragment); 
    } 

ft = getFragmentManager().beginTransaction(); 

if(getResources().getConfiguration().orientation == 1){  
    ft.add(R.id.calendrier_planifications, new Planifications()); 
} 

else if(getResources().getConfiguration().orientation == 2){ 

    ft.add(R.id.calendrier_planifications, new Planifications()); 
    ft.add(R.id.calendrier_calendrierhebdomadaire, new Test()); 

} 

ft.commit(); 

我使用兩種不同的佈局文件:包含兩個LinearLayouts,佈局端口/含有1的LinearLayout

calendrier.xml的問題是,當我開始佈局/ calendrier.xml我的應用程序在橫向模式(方向== 2),並將其切換到縱向模式(方向== 1),我仍然有我的片段測試執行的方法。

如何完全刪除測試片段?

+0

有人可以幫我嗎? – Blacksword888

回答

0

您已經創建了兩個事務,但只提交了第二個事務。嘗試刪除此行:

ft = getFragmentManager().beginTransaction(); 
+0

我試過你說的,它沒有工作 我試圖提交併創建一個新的,它也沒有工作 – Blacksword888