2015-08-04 18 views
5

mam ten układ:Fragment nie zatrzymuje podgląd kamery

<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> 

A w mojej działalności mam to:

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(); 

Teraz co to robi, to uruchomić podgląd kamery (na czystym fragmencie podglądu), odłącza go i zamienia na inny podgląd, który jest moim zmodyfikowanym podglądem, a następnie ponownie dołącza go (czysty fragment podglądu).

Odłącz(): Odłącz podany fragment od interfejsu użytkownika. Jest to ten sam stan, co w przypadku umieszczenia go na tylnym stosie: fragment jest usuwany z interfejsu użytkownika, jednak jego stan nadal jest aktywnie zarządzany przez menedżera fragmentów.

Odłączam go, ale podgląd kamery nadal się świeci?

Ponieważ po wymianie widać zmodyfikowany podgląd. Czy jest to spowodowane metodą attach()?

+0

bardzo złym tytułem. –

+0

Fragment, który na koniec powinieneś nazwać ostatnim przed komendą. Nie należy również wywoływać metody addToBackStack, jeśli nie chcesz jej zachować http://developer.android.com/intl/ko/training/basics/fragments/fragment-ui.html – Want2bExpert

+0

Uważam, że to dość zabawne, że wszyscy mówiąc to samo, a jednak osoba, która prosi, nigdy nie odpowiedziała. I ... StackOverflow mówi powyżej "To pytanie nie otrzymało wystarczającej uwagi". Może Ironia? –

Odpowiedz

3

Fragment, który chcesz na końcu, powinien zostać wywołany jako ostatni przed komendą. Nie należy również wywoływać metody addToBackStack, jeśli nie chcesz go aktualizować developer.android.com/intl/ko/training/basics/fragments/

2

Dlaczego zastępujesz fragment, a następnie dołączasz oderwany fragment?

myślę, że należy zrobić tak:

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(); 
2

Detach(): Detach the given fragment from the UI. Ale myślę, że chodzi o to, po prostu dodaje cleanPreviewFragment i nie popełnić jeszcze, więc cleanPreviewFragment nie był w hierarchii UI jeszcze, więc detach i attach nic nie zrobią.

Może to wola działa:

// 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(); 
3

Miałem ten sam problem w tygodniu ago.But I rozwiązać to za pomocą .replace metoda().

Spróbuj poniżej i mam 100% pewności, że problem zostanie rozwiązany.

// 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(); 

to dla odniesienia do powyższego kodu uprzejmie otwartym poniżej linku

http://developer.android.com/training/basics/fragments/fragment-ui.html

tak Teraz Twój kod będzie wyglądał

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(); 
Powiązane problemy