【发布时间】:2015-01-13 14:44:19
【问题描述】:
使用片段我一直使用replace() 处理我的事务,但我希望我不必再保存实例状态来恢复片段的视图并防止在返回该片段时重新加载。所以,我决定与add() 合作。问题是当我添加另一个片段时,前一个片段视图保留在后台,这很好(这是我所期望的行为),但问题是我实际上可以与后台视图交互。 示例:
片段 A 有一个 Button
片段 B 有一个 TextView
当我添加 Fragment A 后添加 Fragment B 时,我可以点击 Fragment A's Button,甚至停留在 Fragment B 的 视图上。
我正在使用:
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction().
add(getRootViewContainer(),fragment,fragment.getClass().getSimpleName());
if (shouldGoBack)
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
其中getRootViewContainer() 返回我用作活动主容器的FrameLayout 的id。
现在,这真的是add() 的默认行为吗?
如果是这样,是否有适当的方法来避免这种情况,或者只需要使用replace()?
【问题讨论】:
-
您找到解决方案了吗?如果你还没有,试试下面的一些。我确信我的会工作,因为我自己使用它。
标签: android android-layout android-activity android-fragments fragmenttransaction