【发布时间】:2017-02-12 08:09:37
【问题描述】:
我的问题如下:
我有一个应用程序,它有一个主要活动,其中有 是一个 frameLayout 容器 (containerViewId),我用来在菜单导航器选择时显示/膨胀不同的片段。
我实际上做的是在 onCreate() 活动上创建所有片段对象并将其添加到 frameLayout:
伪代码:
// 在主活动中 onCreate()
Fragment1 fragment_1 = new Fragment1();
Fragment2 fragment_2 = new Fragment2()
Fragment3 fragment_3 = new Fragment3()
fragmentTransaction.add(containerViewId, fragment_1, "frag_1");
fragmentTransaction.add(containerViewId, fragment_2, "frag_2");
fragmentTransaction.add(containerViewId, fragment_3, "frag_3");
// hide fragments 2&3, so only fragment_1 is showed
fragmentTransaction.hide(fragment_2);
fragmentTransaction.hide(fragment_3);
.commit()..
现在,当用户从导航视图中选择特定选项时,我只是 隐藏当前显示的fragment,并将需要的fragment显示如下(例如从fragment_1切换到fragment_2):
fragmentTransaction.hide(fragment_1);
fragmentTransaction.show(fragment_2);
fragmentTransaction.commit();
一切都按预期工作(片段已成功切换),但我有时观察到一段时间后(当我将应用程序从后台移到前面时)以前的片段突然显示/重叠在一起(即使我总是隐藏当前片段并显示下一个)
我错过了什么吗? 为什么会突然显示我之前的视图?
谢谢:-)
【问题讨论】:
标签: android fragment android-framelayout