完全破坏片段Android

时间:2013-08-05 10:09:37

标签: android-fragments

我有一项活动,我想根据设备的方向更改片段。所以这是我的代码:

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();

我使用两个不同的布局文件:layout / calendrier.xml包含两个LinearLayouts,layout-port / calendrier.xml包含1个LinearLayout

问题是当我以横向模式启动我的应用程序(orientation == 2)并将其切换到纵向模式(orientation == 1)时,我仍然有我的片段测试执行的方法。

如何完全删除测试片段?

1 个答案:

答案 0 :(得分:0)

您已创建了两个事务,但只提交了第二个事务。尝试删除此行:

ft = getFragmentManager().beginTransaction();