如何取消 FragmentTransaction 提交
How to cancel FragmentTransaction commit
我必须为用户提供在我的应用中捕获视频的选项,但我不希望在视频未录制时显示预览(或拍摄 space)。
因此,我基于 camera2video google 示例使用 FragmentTransaction
构建了一个浮动预览。
我的 class 变量是:
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)
,但还是不行。
所以,问题是我如何 hide\show 预览?
谢谢!
您混淆了一些术语。
提交后,片段系统仅 "executed" 事务。在调用 commit()
之前没有任何反应。
所以你确实执行了两个不同的事务,一个用于显示,另一个用于隐藏。
显示:
getFragmentManager().beginTransaction().show(camera2VideoFragment).commit();
隐藏:
getFragmentManager().beginTransaction().hide(camera2VideoFragment).commit();
我必须为用户提供在我的应用中捕获视频的选项,但我不希望在视频未录制时显示预览(或拍摄 space)。
因此,我基于 camera2video google 示例使用 FragmentTransaction
构建了一个浮动预览。
我的 class 变量是:
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)
,但还是不行。
所以,问题是我如何 hide\show 预览? 谢谢!
您混淆了一些术语。
提交后,片段系统仅 "executed" 事务。在调用 commit()
之前没有任何反应。
所以你确实执行了两个不同的事务,一个用于显示,另一个用于隐藏。
显示:
getFragmentManager().beginTransaction().show(camera2VideoFragment).commit();
隐藏:
getFragmentManager().beginTransaction().hide(camera2VideoFragment).commit();