【问题标题】:Android Fragment transaction - replacing fragment leaves the old one with strange stateAndroid Fragment 事务 - 替换 Fragment 会使旧的碎片状态奇怪
【发布时间】:2015-05-19 21:20:10
【问题描述】:

我有奇怪的行为。每次我在 Activity 中替换相同类型的片段(使用事务)时,都会将新的片段实例添加到片段列表中。旧实例在 Fragment Manager 中保持活动状态,并且在屏幕上显示方向更改后(尽管不可点击)。

我的活动布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

-->

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->

<android.support.v4.widget.DrawerLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.cubesoft.zenfolio.moments.app.activity.MainActivity" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="com.cubesoft.zenfolio.moments.app.fragment.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

<fragment
    android:id="@+id/fragmentConnectionStatus"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    class="com.cubesoft.zenfolio.fragment.ConnectionStatusFragment" />

我改变片段的代码:

@Override
public void onNavigationDrawerItemSelected(int position) {
    mCurrentDrawerPosition = position;
    // update the main content by replacing fragments


    List<Fragment> fragmemts = getSupportFragmentManager().getFragments();

    switch (position) {
    case 0:{
        if ( getMyApplication().getGroupModel().getUsersCount() > 0  ) {
            final Fragment fragment = UserSelectionFragment.newInstance();

            final FragmentManager fragmentManager = getSupportFragmentManager();
            //fragmentManager.popBackStack();
            final FragmentTransaction tr = fragmentManager
                    .beginTransaction();

            tr.replace(R.id.container, fragment);
            tr.addToBackStack(null);
            tr.setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out);
            tr.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            tr.commit();

        } else {
            final Fragment fragment = MomentsFragment.newInstance(mUsername);

            final FragmentManager fragmentManager = getSupportFragmentManager();
            //fragmentManager.popBackStack();

            final FragmentTransaction tr = fragmentManager
                    .beginTransaction();
            tr.replace(R.id.container, fragment);
            //tr.setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out);
            tr.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            tr.addToBackStack(null);

            tr.commit();
        }





        break;
    }

    case 1: {
        final Fragment fragment = DownloadFragment.newInstance();
        final FragmentManager fragmentManager = getSupportFragmentManager();
        //fragmentManager.popBackStack();
        final FragmentTransaction tr = fragmentManager
                .beginTransaction();
        tr.replace(R.id.container, fragment);
        tr.addToBackStack(null);

        tr.commit();
        break;
    }

    /*case 2:
        fragment = UserSelectionFragment.newInstance();
        break;*/

    }


}

在多次方向更改后,UserSelectionFragment 显示不正确,UserSelectionFragment 的旧实例保留在 Fragment List 中,但它们的 View 对象为空。

怎么了?

【问题讨论】:

  • 你发现了吗?

标签: android fragment orientation-changes


【解决方案1】:

尝试删除

 tr.addToBackStack(null);

对我来说,这会将您的片段添加到后台堆栈中,这就是它们保留在片段列表中的原因。他们的观点是无效的,因为他们在去背景之前已经被破坏了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    相关资源
    最近更新 更多