繁体   English   中英

Android,片段事务的奇怪行为replace()

[英]Android, strange behavior of fragment transaction replace()

我想使用多窗格布局来扩大屏幕。 数据以SQL持久化,每个片段都会获取正确的数据。 额外的布局xml文件位于资源目​​录文件夹中。(即layout-w500dp)
但我有一些奇怪的行为。
它只在我选择一些东西然后按后退按钮后才起作用。
Atm我使用最多两个FrameLayouts但后来我想用四个来做。 我检查最深选择的级别并相应地分配片段。 (这里只有lvl 1,但后来我需要选择lvl3)。
这就是我想要实现的目标。

图片1

这在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. 以纵向和横向开始应用程序= 1个片段(这是所需的行为)
  2. 在肖像中进行选择=没有任何反应!!!! (这是问题)
  3. 使用正确的选择切换到Landscape = 2碎片(正确的行为)
    (如果我在横向中进行初始选择,我需要旋转到纵向并再次返回)
  4. 用正确的数据切换到Portrait = LvL 2 Fragment(正确的行为)
  5. 按Back Button = LvL 1 Fragment(正确的行为)
  6. 从现在开始,我可以在纵向和横向之间切换,我可以选择适合所有方向的项目。 甚至在横向上的背景中,当选择被取走时,仅显示具有lvl 1的一个片段。

我为什么会这样做?
这是第一个正确的方法吗?
考虑到我想将其扩展到更高级别和screenWidths!
即:

图像2

会在这里正确地进行后备功能吗? 如果有人需要额外的信息,只需说,我会很乐意添加它!

愚蠢的错误。 我将选择保存在应用程序类中,并在onCreate但我需要在getLowestSelection()方法中获取当前选择之前重新实例化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM