我想为更宽的屏幕使用多 Pane 布局。数据使用 SQL
持久化,每个 fragment 获取正确的数据。额外的布局 xml 文件位于资源目录文件夹中。(即 layout-w500dp)
但我有一些奇怪的行为。
它似乎只有在我选择了一些东西然后按下后退按钮后才起作用。
Atm 我最多使用两个 FrameLayouts 但后来我想用四个来做。
我检查最深选择的级别并相应地分配 fragment 。 (这里只有 1 级,但后来我需要选择 3 级)。
这是我想要实现的目标。
这会在 onCreate
和做出选择时调用。
private void setScreens(){
int i = getLowestSelection();//returns 0 when nothing is selected.
//And 1 if selection is made in lvl1 ...
int p = 1;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (findViewById(R.id.fragtwo) != null) {
p = 2;
if (i == 1){
SectionsScreen secondFragment = new SectionsScreen();
transaction.replace(R.id.fragtwo,secondFragment);
}
}
if (findViewById(R.id.fragone) != null) {
if(p == 2){
if (i == 0 ){
StatuteScreen statuteScreenFragment = new StatuteScreen();
transaction.replace(R.id.fragone,statuteScreenFragment);
}
}
if (p == 1){
if (i == 0){
StatuteScreen statuteScreenFragment = new StatuteScreen();
transaction.replace(R.id.fragone,statuteScreenFragment);
}
else if (i == 1){
SectionsScreen sectionsScreenFragment = new SectionsScreen();
transaction.replace(R.id.fragone,sectionsScreenFragment);
}
}
}
transaction.addToBackStack(null);
transaction.commit();
}
只有在我执行以下操作时它才有效。
- 启动应用程序 = 1 个纵向和横向 fragment (这是所需的行为)
- 在纵向中进行选择 = 没有任何反应 !!!! (这里是问题所在)
- 切换到风景 = 2 个具有正确选择(正确行为)的 fragment
(如果我在风景中进行初始选择,我需要旋转到肖像并再次旋转) - 切换到纵向 = 具有正确数据的 LvL 2 fragment (正确的行为)
- 按下后退按钮 = LvL 1 fragment (正确行为)
- 从现在开始,我可以在纵向和横向之间切换,并且我可以在所有方向上选择项目的正确行为。 Even on backpress in landscape showing only one fragment with lvl 1 when selection is taken away.
为什么我会出现这种行为?
这首先是正确的方法吗?
考虑到我想将其扩展到更多级别和屏幕宽度!
即:
这里的 backstack 能正常工作吗?如果有人需要其他信息,请说出来,我很乐意添加!
最佳答案
愚蠢的错误。我将选择保存在一个应用程序类中,并在 onCreate
中实例化它,但我需要在 getLowestSelection()
方法中获取当前选择之前重新实例化
关于java - Android, fragment 事务替换的奇怪行为(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17261284/