【发布时间】:2016-06-14 06:12:52
【问题描述】:
在我的示例中,我尝试了解片段的commitAllowingStateLoss() 和commit() 方法之间的区别。现在我知道如果在活动保存实例之后使用commit() 将出现IllegalArgumentException(例如,当活动在后台时执行commit())。
但是出现了另一个问题,片段重叠。我不知道为什么,也无法解决问题。
我的代码如下。
在 FragmentMainActivity 中有两个用于切换片段的按钮。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_fragment1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fragment 1" />
<Button
android:id="@+id/btn_fragment2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fragment 2" />
</LinearLayout>
<FrameLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"></FrameLayout>
id 为“root”的框架布局是片段的容器。
public class FragmentMainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_main);
findViewById(R.id.btn_fragment1).setOnClickListener(this);
findViewById(R.id.btn_fragment2).setOnClickListener(this);
}
PlusOneFragment plusOneFragment;
PlusTwoFragment plusTwoFragment;
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.btn_fragment1) {
findViewById(R.id.root).postDelayed(new Runnable() {
@Override
public void run() {
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (getSupportFragmentManager().findFragmentByTag("tag1") == null) {
plusOneFragment = PlusOneFragment.newInstance("1", "2");
transaction.add(R.id.root, plusOneFragment, "tag1");
} else {
plusOneFragment = (PlusOneFragment) getSupportFragmentManager().findFragmentByTag("tag1");
}
if (getSupportFragmentManager().findFragmentByTag("tag2") != null) {
plusTwoFragment = (PlusTwoFragment) getSupportFragmentManager().findFragmentByTag("tag2");
transaction.hide(plusTwoFragment);
}
transaction.show(plusOneFragment).commitAllowingStateLoss();
}
}, 3000);
} else if (i == R.id.btn_fragment2) {
findViewById(R.id.root).postDelayed(new Runnable() {
@Override
public void run() {
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (getSupportFragmentManager().findFragmentByTag("tag2") == null) {
plusTwoFragment = PlusTwoFragment.newInstance("1", "2");
transaction.add(R.id.root, plusTwoFragment, "tag2");
} else {
plusTwoFragment = (PlusTwoFragment) getSupportFragmentManager().findFragmentByTag("tag2");
}
if (getSupportFragmentManager().findFragmentByTag("tag1") != null) {
plusOneFragment = (PlusOneFragment) getSupportFragmentManager().findFragmentByTag("tag1");
transaction.hide(plusOneFragment);
}
transaction.show(plusTwoFragment).commitAllowingStateLoss();
}
}, 3000);
}
}
}
首先我点击button1显示fragment1,然后点击button2显示fragment2,效果很好。完成这些操作后,我再次单击button1,然后单击主页,因此应用程序在后台运行。为了模拟内存不足的场景,然后我终止了该进程。所有这些操作都发生在 3 秒内。
最后,我从最近的记录(重新创建的活动)中打开应用程序,发现两个片段相互重叠。这是怎么发生的?(为什么当进程在前台时问题没有发生?)我该怎么处理这个问题?
【问题讨论】:
-
给你的片段一个背景颜色(比如白色)并再次检查。你的片段没有被删除。也尝试删除方法而不是添加。如果有帮助,请告诉我
-
我将此添加为答案。请接受。
-
是的,添加背景颜色会有所帮助。但我不知道为什么会这样。如果进程没有被杀死,它在没有背景颜色的情况下运行良好。
标签: android android-fragments overlap