【问题标题】:FragmentTransaction.remove() not removing fragment only while SwipeRefreshLayout is activeFragmentTransaction.remove() 仅在 SwipeRefreshLayout 处于活动状态时才删除片段
【发布时间】:2016-10-29 07:08:54
【问题描述】:

我的问题的要点:

我有多个按顺序添加且未重新排序的片段。几乎所有这些都将 SwipeRefreshLayouts 作为其根视图。当用户触发 onBackPressed 时,应删除最后一个片段。如果打开一个新片段,则隐藏最后一个片段。

如果 SwipeRefreshLayout 处于活动状态(即刷新状态设置为 true 并且调用了 onRefresh)并且用户弹出当前片段,则视图会一直卡住,直到应用重新启动。我已经确认 onPause() 和 onDestroy() 确实被调用了。我已经在每种类型的 Fragment 上对此进行了测试,所以我几乎可以确定问题在于我如何移除 Fragment。

我的代码:

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

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:

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

【问题讨论】:

    标签: android android-fragments swiperefreshlayout


    【解决方案1】:

    经过数小时的搜索,我找到了related thread。两种解决方案都对我有用,尽管我认为我现在更喜欢 FrameLayout 包装器。这似乎是android本身的一个错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多