gpt4 book ai didi

Android fragment 事务 - 替换 fragment 会使旧 fragment 处于奇怪状态

转载 作者:行者123 更新时间:2023-11-29 01:32:42 26 4
gpt4 key购买 nike

我有奇怪的行为。每次我在 Activity 中替换相同类型的 fragment (使用事务)时,新的 Fragment 实例都会添加到 Fragment List 中。旧实例在 fragment 管理器中保持 Activity 状态,并且在方向更改后在屏幕上可见(尽管不可点击)。

我的 Activity 布局:

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

我的改变 fragment 的代码:

@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 对象为空。

怎么了?

最佳答案

尝试删除

 tr.addToBackStack(null);

对我来说,这会将您的 fragment 添加到后台堆栈,这就是将它们保留在您的 fragment 列表中的原因。它们的 View 为空,因为它们在进入背景之前已被销毁。

关于Android fragment 事务 - 替换 fragment 会使旧 fragment 处于奇怪状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30325996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com