I have this layout:
<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>
And in my activity I got this:
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();
Now what this does, it start a camera preview (on the clean preview fragment), detaches it and replaces it with another preview which is my modified preview and then attaches it (the clean preview fragment) again.
Detach(): Detach the given fragment from the UI. This is the same state as when it is put on the back stack: the fragment is removed from the UI, however its state is still being actively managed by the fragment manager.
So I detach it, but the camera preview still goes on?
Because when the replace happens I can see a modified preview. Is that caused by the attach() method?
The fragment you want in the end should be called last before comit. Also don't call addToBackStack method if you don't want to keep it developer.android.com/intl/ko/training/basics/fragments/
I had The same problem a week ago.But I solved this by using .replace() method.
Try below and i am 100% sure that you problem will be solved.
// 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();
this for the reference of above code kindly open below link
http://developer.android.com/training/basics/fragments/fragment-ui.html
so Now your code will look like
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With