0

The gist of my problem:

I have multiple fragments that are added sequentially and not re-ordered. Almost all of them have SwipeRefreshLayouts as their root views. When the user triggers onBackPressed, the last fragment should be removed. If a new fragment is opened, the last fragment is hidden.

In the event that the SwipeRefreshLayout is active (ie refresh state set to true and onRefresh is called) and the user pops off the current fragment, the view remains stuck until an app restart. I've confirmed that onPause() and onDestroy() do get called. I have tested this across each type of my fragments, so I'm nearly sure the problem lies in how I remove fragments.

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

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

Main activity XML:

<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>
Zach
  • 447
  • 6
  • 16

1 Answers1

0

So I found a related thread after hours of searching. Both solutions worked for me, although I think I prefer the FrameLayout wrapper for now. This seems to be a bug in android itself

Zach
  • 447
  • 6
  • 16