2016-10-29 2 views
0

L'essentiel de mon problème:FragmentTransaction.remove() ne supprimant pas seulement tout fragment SwipeRefreshLayout est actif

je plusieurs fragments qui sont ajoutés de façon séquentielle et pas ré-ordonné. Presque tous ont SwipeRefreshLayouts en tant que leurs vues racine. Lorsque l'utilisateur déclenche onBackPressed, le dernier fragment doit être supprimé. Si un nouveau fragment est ouvert, le dernier fragment est caché.

Dans le cas où le SwipeRefreshLayout est actif (à savoir l'état de rafraîchissement réglé sur vrai et OnRefresh est appelé) et l'utilisateur éjecte du fragment courant, la vue reste bloqué jusqu'à ce qu'un redémarrage de l'application. J'ai confirmé que onPause() et onDestroy() sont appelés. J'ai testé cela à travers chaque type de mes fragments, donc je suis presque sûr que le problème réside dans la façon dont j'enlève les fragments.

Mon code:

//I know this is a misnomer. I just changed the inside from 
//addToBackStack which got me the same effect 
protected void addToBackStack(BaseFragment fragment){ 
    FragmentTransaction ft = fragMan.beginTransaction(); 
    if(fragCount > 0) 
     ft.hide(peekBackStack()); 
    ft.add(R.id.fragment_container, fragment, Integer.toString(fragCount++)).commit(); 
    invalidateOptionsMenu(); 
} 

protected BaseFragment peekBackStack(){ 
    return (BaseFragment)(fragMan.findFragmentByTag(Integer.toString(fragCount - 1))); 
} 

@Override 
public void onBackPressed(){ 
    if (fragCount <= 1) { 
     //...do stuff & finish 
    } else { 
     BaseFragment fragment = peekBackStack(); 
     if(fragment != null) { 
      if (fragment.isWeb() && ((WebFragment) fragment).canGoBack()) 
       ((WebFragment) fragment).goBack(); 
      else 
       popActive(); 
     } 
     else 
      finish(); 
    } 
} 

public void popActive(){ 
    BaseFragment remove = peekBackStack(); 
    fragCount--; 
    BaseFragment show = peekBackStack(); 
    fragMan.beginTransaction().remove(remove).commit(); 
    Log.d(TAG, ""+remove.isVisible()); 
    fragMan.beginTransaction().show(show).commitNow(); 
    show.initAppBar(); 
} 

intérieur BaseFragment.java

@Override 
@NonNull 
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
         @Nullable Bundle state){ 
    ViewGroup root = (ViewGroup) inflater.inflate(getLayoutResource(), container, false); 
    if(root instanceof SwipeRefreshLayout) { 
     swipeRefreshLayout = (SwipeRefreshLayout) root; 
     swipeRefreshLayout.setOnRefreshListener(this); 
    } 
    return root; 
} 

@Override 
public void onDestroy(){ 
    super.onDestroy(); 
    if(swipeRefreshLayout != null) 
     swipeRefreshLayout.destroyDrawingCache(); 
} 
... 

WebFragment XML:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/swipe_refresh_submission_link" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<com.github.ksoichiro.android.observablescrollview.ObservableWebView 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/observable_web" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/toolbar" 
    android:scrollbarStyle="outsideOverlay" 
    android:fadeScrollbars="true"> 

</com.github.ksoichiro.android.observablescrollview.ObservableWebView> 

XML Activité principale:

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/parent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbarLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      ... 
     </android.support.design.widget.AppBarLayout> 
     <RelativeLayout 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/appbarLayout"> 
     </RelativeLayout> 
    </RelativeLayout> 
</android.support.design.widget.CoordinatorLayout> 

Répondre

0

Je trouve un related thread après des heures de recherche. Les deux solutions ont fonctionné pour moi, bien que je pense que je préfère l'emballage de FrameLayout pour l'instant. Cela semble être un bug dans Android lui-même