消失的碎片(未经请求而删除)



我有一个简单的布局,其中包含两个片段。两个片段中的一个会定期替换自己,另一个片段将在我使用它的整个过程中保持一致。出于某种原因,删除/添加它工作得很好,但替换它会导致另一个片段消失。我从调试代码中知道片段只是被分离,没有明显的原因。为什么会这样?如果我使用后者,我必须非常小心地确保问题区域首先消失......

这些代码行来自 FragmentActivity 类。

        fragManager.beginTransaction()
        .replace(R.id.QuestionArea, getQuestionPostView(), "QuestionArea")
        .commit();
        fragManager.beginTransaction()
        .remove(fragManager.findFragmentById(R.id.QuestionArea))
        .add(R.id.QuestionArea, getQuestionPostView(), "QuestionArea")
        .commit();

以下是我希望是其他相关代码:

主显示器的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >
    <com.pearsonartphoto.FontFitTextView
        android:id="@+id/Name"
        style="@style/bigText"
        android:background="@color/blue"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"/>
    <TextView android:id="@+id/Instructions"
        style="@style/medText"
        android:layout_width="fill_parent"
        android:layout_below="@id/PowerName"
        />
    <fragment
        android:name="com.pearsonartphoto.NumberDisplay"
        android:gravity="center"
        android:id="@+id/QuestionArea"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Instructions"/>
    <fragment
        android:name="com.pearsonartphoto.AbstractAnswerFragment"
        android:id="@+id/AnswerArea"
        style="@style/bigText"
        android:gravity="center"
        android:layout_below="@+id/QuestionArea"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="530sp"/>
</RelativeLayout>

FragmentActivity 的一部分,用于设置 AnswerArea

protected void setAnswerPad(AbstractAnswerFragment pad)
{
    fragManager.beginTransaction()
        .replace(R.id.AnswerArea, pad, "AnswerArea")
        .commit();
    fragManager.executePendingTransactions();
    answerPadToAnswer=pad.getAnswerKey();
}

第一次(从 FragmentActivity 开始)设置问题区域。

    fragManager.beginTransaction()
    .replace(R.id.QuestionArea, getQuestionPreView(), "QuestionArea")
    .commit();

函数 getPostView() 和 getPreView()

@Override
Fragment getQuestionPreView() {
    NumberDisplay display=(NumberDisplay)manager.findFragmentById(R.id.QuestionArea);
    display.setNumber(-1);
    return display;
}
@Override
Fragment getQuestionPostView() {
    NumberDisplay display=(NumberDisplay)manager.findFragmentById(R.id.QuestionArea);
    display.setNumber(answer);
    return display;
}

您的问题很可能来自尝试在运行时更改静态片段(在 xml 布局中声明的片段),这是您不应该这样做的。如果您打算替换、删除FragmentActivity中的这些片段,您应该在 xml 布局中的位置放置一个包装器布局(如 FrameLayout),并将该容器用于将来的片段事务。查看其中一位Android工程师的答案。

需要

做一些事情才能完成这项工作:

  1. 关键是将我的主要XML代码更改为FrameLayout
  2. 必须创建一些较小的片段,而以前我不必创建它们。

新的主 XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >
    <com.pearsonartphoto.FontFitTextView
        android:id="@+id/PowerName"
        style="@style/bigText"
        android:background="@color/blue"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"/>
    <TextView android:id="@+id/Instructions"
        style="@style/medText"
        android:layout_width="fill_parent"
        android:layout_below="@id/PowerName"
        />
    <FrameLayout 
        android:gravity="center"
        android:id="@+id/QuestionArea"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Instructions"/>
    <FrameLayout 
        android:id="@+id/AnswerArea"
        style="@style/bigText"
        android:gravity="center"
        android:layout_below="@+id/QuestionArea"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="530sp"/>
</RelativeLayout>

最新更新