我已经解决了这个问题,并执行了以下解决方案来解决它,但还无法解决这个问题。我还尝试使用fragmentpageradapter
代替fragmentstatepageradapter
,但仍面临同样的问题。
java.lang.IllegalStateException: Fragement no longer exists for key f1: index 3
当我回到包含viewpager的片段时,会发生这种崩溃。有什么想法吗?
首先将productdetail片段推送到“maincontainer”
public void switchToProductDetail(SearchHelper searchHelper, Product product)
{
ProductDetailFragment productDetailFragment = new ProductDetailFragment();
productDetailFragment.product = product;
productDetailFragment.searchHelper = searchHelper;
setNewFragment(productDetailFragment, R.id.mainContainer, "ProductDetail", true, true, true, true);
}
然后如果用户没有登录并按下添加到fav按钮,我将注册片段推送到“window”容器。
public void switchtoSignIn(SignInSignUpFragment.SignInCompleteCallBack callBack)
{
SignInSignUpFragment signInSignUpFragment = new SignInSignUpFragment();
signInSignUpFragment.callBack = callBack;
setNewFragment(signInSignUpFragment, R.id.windowFL, "SignIn", true, true, true, true);
}
主要活动xml(包含maincontainer和windowFL)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/windowFL"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/headerContainer" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainContainer"/>
</LinearLayout>
<LinearLayout
android:id="@+id/left_menu_frame"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginRight="@dimen/menumargin"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff" />
</android.support.v4.widget.DrawerLayout>
setnewfragment方法是:
@SuppressLint("CommitTransaction")
public void setNewFragment(final Fragment fragment, final int containerId, final String tag, final boolean shouldAddBackStack, final boolean shouldReplace, final boolean shouldAnimation, final boolean shouldDrawerClose) {
if(isAnyMenuOpen() && shouldDrawerClose)
{
drawerLayout.closeDrawer(GravityCompat.START, true);
drawerClosedCallBack = new DrawerClosedCallBack() {
@Override
public void onDrawerClosed() {
setNewFragment(fragment, containerId, tag, shouldAddBackStack, shouldReplace, shouldAnimation, false);
}
};
return;
}
if (fragmentManager == null)
{
fragmentManager = getSupportFragmentManager();
}
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shouldAnimation)
{
if(fragment instanceof SignInSignUpFragment)
{
fragmentTransaction.setCustomAnimations(R.anim.slide_in_bottom, R.anim.fade_out, R.anim.fade_in, R.anim.slide_out_bottom);
}
else
{
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
}
}
if (shouldReplace)
{
fragmentTransaction.replace(containerId, fragment);
}
else
{
fragmentTransaction.add(containerId, fragment);
}
if(shouldAddBackStack)
{
fragmentTransaction.addToBackStack(tag);
}
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commitAllowingStateLoss();
}