我的行为很奇怪。每次我在Activity(使用事务)中替换相同类型的片段时,新的Fragment实例都会添加到Fragment List中。旧实例在片段管理器中保持活动状态,并且在屏幕上可以看到方向更改后(虽然不可点击)。
我的活动布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
- &GT;
<!-- 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的旧实例仍保留在片段列表中,但其View对象为空。
有什么问题?
答案 0 :(得分:1)
尝试删除
tr.addToBackStack(null);
对我来说,这会将你的背叛添加到后台堆栈,这就是为什么它们被保存在你的碎片列表中的原因。他们的观点是空的,因为他们在去背景之前就被摧毁了。