首页 > 解决方案 > 片段被绘制在彼此之上

问题描述

场景是这样的......我单击一个加载下一个片段的按钮。虽然动画仍在播放,但我点击了最近的应用程序按钮。显示最近的应用程序屏幕后,我返回我的应用程序。

然后我看到两个片段相互重叠。好像Android忘记删除以前的片段。我单击返回以删除所有片段,直到到达此 FragmentActivity 中加载的第一个片段。我记录了从 FragmentManager 获得的所有片段,它只显示一个。但我仍然看到两个片段一个一个叠加。应该删除的一个没有响应触摸事件,而另一个则按应有的方式响应。

这是Android错误还是我的错?有没有办法解决这个问题或防止它发生?

它不会每次都发生。这就是我加载片段的方式

public void loadNewFragment(AnimFragment newFragment, boolean addToSTack, boolean animate, String tag) {

    if (newFragment != null) {

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.setCustomAnimations(R.anim.fragment_enter_from_right, R.anim.fragment_exit_to_left, R.anim.fragment_enter_from_left, R.anim.fragment_exit_to_right);

        transaction.replace(R.id.fragment_container, newFragment, tag);

        if (addToSTack) {
            transaction.addToBackStack(tag);
        }

        int transactionId = transaction.commit();
        newFragment.setTransactionId(transactionId);
    }

}

然后我用背压将它们移除

getSupportFragmentManager().popBackStackImmediate();

标签: androidandroid-fragmentsandroid-fragmentactivity

解决方案


在我看来,“下方”片段是onPause()状态,它仍然存在且可见。

尝试在前端片段的根布局上使用白色背景,以避免显示下面的片段。此外,前端片段的根布局大小(宽度和高度)应为 match_parent。

检查此站点以了解Fragment生命周期。

https://google-developer-training.gitbooks.io/android-developer-advanced-course-concepts/unit-1-expand-the-user-experience/lesson-1-fragments/1-2-c-fragment-生命周期和通信/1-2-c-fragment-lifecycle-and-communications.html


推荐阅读