Android fragments 彻底摧毁碎片
我有一个活动,我想根据设备的方向更改片段。这是我的代码:Android fragments 彻底摧毁碎片,android-fragments,Android Fragments,我有一个活动,我想根据设备的方向更改片段。这是我的代码: FragmentTransaction ft = getFragmentManager().beginTransaction(); if(getFragmentManager().findFragmentById(R.id.calendrier_calendrierhebdomadaire) != null){ Fragment fragment = getFragmentManager().findFragmentB
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包含两个LinearLayout,layout port/calendrier.xml包含一个LinearLayout
问题是,当我以横向模式(方向==2)启动应用程序并将其切换到纵向模式(方向==1)时,仍然会执行片段测试的方法
如何完全删除测试片段?您创建了两个事务,但只提交了第二个事务。尝试删除此行:
ft = getFragmentManager().beginTransaction();
有人能帮帮我吗?我试过你说的,但没用我试着去承诺并创建一个新的,也没用