Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment detach method doesn't stop camera preview

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?

like image 338
Trt Trt Avatar asked Aug 04 '15 19:08

Trt Trt


2 Answers

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/

like image 59
Want2bExpert Avatar answered Sep 29 '22 04:09

Want2bExpert


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();
like image 23
Moubeen Farooq Khan Avatar answered Sep 29 '22 02:09

Moubeen Farooq Khan