如何取消FragmentTransaction提交

问题描述 投票:0回答:1

我必须为用户提供在我的应用程序中捕获视频的选项,但我不希望在视频未录制时出现预览(或占用空间)。

因此,我基于

camera2video
谷歌示例,使用 FragmentTransaction 构建浮动预览。

我的类变量是:

FragmentTransaction fm = null;
Camera2VideoFragment camera2VideoFragment;

我创建一个实例并在

OnCreate
方法中初始化相机:

camera2VideoFragment = Camera2VideoFragment.newInstance();
if (null == savedInstanceState) {
    fm = getFragmentManager().beginTransaction()
            .replace(R.id.container, camera2VideoFragment);
}

我想使用菜单方法(onOptionsItemSelected)显示和隐藏预览(片段):

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
    }

我也尝试过

fm.hide(camera2VideoFragment)
但也不起作用。

那么,问题是,如何隐藏/显示预览?

android fragment fragmenttransaction
1个回答
2
投票

您混淆了一些术语。 事务仅在提交后才由片段系统“执行”。在打电话之前

commit()
什么也没发生。

因此,您必须执行两项不同的交易,一项用于显示,另一项用于隐藏。

显示:

getFragmentManager().beginTransaction().show(camera2VideoFragment).commit();

隐藏:

getFragmentManager().beginTransaction().hide(camera2VideoFragment).commit();
© www.soinside.com 2019 - 2024. All rights reserved.