Android 如何取消碎片事务提交

Android 如何取消碎片事务提交,android,fragment,fragmenttransaction,Android,Fragment,Fragmenttransaction,我必须给用户一个在我的应用程序中捕获视频的选项,但我不希望在视频未录制时显示预览(或占用空间) 因此,我使用基于google示例的FragmentTransaction构建了一个浮动预览 我的类变量是: FragmentTransaction fm = null; Camera2VideoFragment camera2VideoFragment; 我在OnCreate方法中创建一个实例并初始化相机: camera2VideoFragment = Camera2VideoFragment.ne

我必须给用户一个在我的应用程序中捕获视频的选项,但我不希望在视频未录制时显示预览(或占用空间)

因此,我使用基于google示例的
FragmentTransaction
构建了一个浮动预览

我的类变量是:

FragmentTransaction fm = null;
Camera2VideoFragment camera2VideoFragment;
我在
OnCreate
方法中创建一个实例并初始化相机:

camera2VideoFragment = Camera2VideoFragment.newInstance();
if (null == savedInstanceState) {
    fm = getFragmentManager().beginTransaction()
            .replace(R.id.container, camera2VideoFragment);
}
我想使用菜单方法(OnOptions ItemSelected)查看和隐藏预览(片段):

我还尝试了
fm.hide(camera2VideoFragment)
,但效果不太好

因此,问题是如何隐藏\显示预览?
谢谢

您混淆了一些术语。 事务只有在您提交后才由片段系统“执行”。在调用
commit()
之前,什么都不会发生

所以您必须执行两个不同的事务,一个用于显示,另一个用于隐藏

展示:

隐藏:

case R.id.action_captureScreen:
    item.setChecked(!item.isChecked());
    if (item.isChecked())
    {
        fm.commit(); // show the preview - working
        // camera2VideoFragment.captureVideo();  // start capture video
    }
    else
    {
        //camera2VideoFragment.captureVideo();  // stop the video and save to file
        fm.detach(camera2VideoFragment);        // hide the preview - NOT WORKING
    }
getFragmentManager().beginTransaction().show(camera2VideoFragment).commit();
getFragmentManager().beginTransaction().hide(camera2VideoFragment).commit();