我有这个布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/DeepSkyBlue"
tools:context=".MainPreviewActivity"
android:weightSum="5">
<FrameLayout
android:id="@+id/clean_preview_fragment_container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3"/>
<ListView
android:layout_height="0dp"
android:layout_weight="2"
android:layout_width="match_parent"
android:id="@+id/lvImageProcessChoices"/>
</LinearLayout>
在我的活动中,我得到了这个:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(savedInstanceState != null){
System.out.println("savedInstanceState is NOT null");
return;
}
CleanPreviewFragment cleanPreviewFragment = new CleanPreviewFragment();
processedPreviewFragment = new ProcessedPreviewFragment();
fragmentTransaction.add(R.id.clean_preview_fragment_container,cleanPreviewFragment);
fragmentTransaction.detach(cleanPreviewFragment);
fragmentTransaction.replace(R.id.clean_preview_fragment_container, processedPreviewFragment);
fragmentTransaction.attach(cleanPreviewFragment);
fragmentTransaction.commit();
现在这样做,它启动了一个摄像头预览(在干净的预览片段上),将其分离并用另一个预览替换它,这是我修改过的预览,然后再次附加它(干净的预览片段)。
Detach():从UI中分离给定的片段。这与将其放在后台堆栈时的状态相同:片段从UI中删除,但片段管理器仍在主动管理其状态。
所以我分开了,但相机预览仍在继续吗?
因为当替换发生时,我可以看到修改后的预览。这是由attach()方法引起的吗?
答案 0 :(得分:3)
最后想要的片段应该在comit之前调用。如果你不想保留它,也不要调用addToBackStack方法developer.android.com/intl/ko/training/basics/fragments /
答案 1 :(得分:3)
一周前我遇到了同样的问题。但是我通过使用.replace()方法解决了这个问题。
尝试下面,我100%确定您的问题将得到解决。
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
这是以上代码的参考,请在链接下面打开
http://developer.android.com/training/basics/fragments/fragment-ui.html
所以现在您的代码看起来像
fragmentTransaction.add(R.id.clean_preview_fragment_container,cleanPreviewFragment);
fragmentTransaction.commit();
fragmentTransaction.detach(cleanPreviewFragment);
fragmentTransaction.replace(R.id.clean_preview_fragment_container, processedPreviewFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
答案 2 :(得分:2)
为什么要替换片段然后附加分离的片段?
我认为你应该这样做:
fragmentTransaction.add(R.id.clean_preview_fragment_container,cleanPreviewFragment);
fragmentTransaction.commit();
fragmentTransaction.detach(cleanPreviewFragment);
fragmentTransaction.replace(R.id.clean_preview_fragment_container, processedPreviewFragment);
fragmentTransaction.commit();
答案 3 :(得分:2)
Detach(): Detach the given fragment from the UI.
但我认为重点是您刚刚添加cleanPreviewFragment
但尚未提交,因此cleanPreviewFragment
尚未在UI层次结构中,因此detach
和attach
会执行什么都没有。
也许这会奏效:
// first, add cleanPreviewFragment to UI
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.clean_preview_fragment_container,cleanPreviewFragment);
fragmentTransaction.commit();
// then create new transition and do your job
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.detach(cleanPreviewFragment);
fragmentTransaction.replace(R.id.clean_preview_fragment_container, processedPreviewFragment);
fragmentTransaction.attach(cleanPreviewFragment);
fragmentTransaction.commit();