2014-01-30 77 views
0

我已经搜索了这个问题,似乎这样的问题还没有被问到。动态片段和屏幕旋转

我有什么

,其具有通过java代码添加(不<frgament> </fragment>)三个片段的应用程序。当设备处于横向模式时,所有三个片段一起显示。另一方面,在肖像模式下,只显示一个。在肖像模式下显示的片段有一个按钮。如果按下该按钮,将触发一个事件,通知监听器(在我的情况下是主要活动),然后用另一个替换片段。一切都在一个活动中进行。

下面是从活动的onCreate():

保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);

 //depending on the orientation this layout file is different. 
    setContentView(R.layout.fragment_layout); 

    //landscapeCase 
    if (getResources().getConfiguration().orientation 
      == Configuration.ORIENTATION_LANDSCAPE) { 


     //adding three fragments into their places 

     FragmentManager fragMgr = getFragmentManager(); 


     FragmentTransaction xact = fragMgr.beginTransaction(); 

     if(fragMgr.findFragmentByTag(FRAG1_TAG)==null){ 

      xact.add(R.id.frame1,new AddNoteFragment(), FRAG1_TAG); 
     } 

     if(fragMgr.findFragmentByTag(FRAG2_TAG)==null){ 

      xact.add(R.id.frame2,new ListNoteFragment(), FRAG2_TAG); 

     } 

     if(fragMgr.findFragmentByTag(FRAG3_TAG)==null){ 
      xact.add(R.id.frame3,new DetailNoteFragment(), FRAG3_TAG); 

     } 


     xact.commit(); 

    } 

    //portrait case 
    if (savedInstanceState == null) { 


     FragmentManager fragMgr = getFragmentManager(); 


     FragmentTransaction xact = fragMgr.beginTransaction(); 

     if(fragMgr.findFragmentByTag(FRAG1_TAG)==null){ 
      xact.add(R.id.frame1,new AddNoteFragment(), FRAG1_TAG); 
     } 
     xact.commit(); 

    } 
} 

下面是处理来自片段按钮按压在人像情况下OnPressed方法:

//frame1, frame2 and frame3 are the layouts inside (fragment_layout.xml) that are dedicated to the    //fragments. 
    //in portrait mode only frame1 is available. 
    @Override 
     public void OnPressed(String TAG) { 
      if(TAG.equals("frag1") && getResources().getConfiguration().orientation 
        == Configuration.ORIENTATION_PORTRAIT){ 



      FragmentManager fragMgr = getFragmentManager(); 
      FragmentTransaction xact = fragMgr.beginTransaction(); 

      if(fragMgr.findFragmentByTag(FRAG2_TAG)==null) { 

       xact.replace(R.id.frame1, new ListNoteFragment(), FRAG2_TAG); 
       } 

      else { 
       //problem occurs here. 
       xact.replace(R.id.frame1, fragMgr.findFragmentByTag(FRAG2_TAG)); 

       } 
      xact.addToBackStack(null); 
      xact.commit(); 

      } 

      } 

的问题

替换片段作品的过程(包括后退按钮)时严格在纵向模式。但是,如果我切换到横向,然后回到纵向,然后按下按钮,一切崩溃(IllegalStateException:无法更改Fragment的容器ID)。

问题从这里茎:旋转时

if(fragMgr.findFragmentByTag(FRAG2_TAG)==null) { 

       xact.replace(R.id.frame1, new ListNoteFragment(), FRAG2_TAG); 
       } 

      else { 
       //problem occurs here. 
       xact.replace(R.id.frame1, fragMgr.findFragmentByTag(FRAG2_TAG)); 

       } 

后,将创建与FRAG2_TAG片段,因此可以追溯到肖像情况下,按下该按钮,则执行else部分。出于某种原因,“它”不喜欢检索已经创建的片段的想法。有趣的是,当我摆脱其他,只保留上半部分一切正常。出于某种原因,当我创建新的碎片时,它会起作用,但当我要求它重新使用旧的碎片时,它不起作用。

问题

任何人都可以解释为什么不允许我再利用已经建立的片段?

是否可以创建只有三个片段,以便它们可以在横向模式下一起显示并在纵向之间切换(全部在单个活动中)?

有关重复使用片段的一些大图想法将不胜感激。

我很抱歉这么长的帖子。

回答

1

您可以重新使用已创建的片段。它崩溃的原因是因为片段会尝试重新创建它自己。你必须检查片段是否已经存在。这里是解决问题的方法:

https://stackoverflow.com/a/9646622/2767703

这家伙解释它完美。

希望这会有所帮助!

+0

感谢您的链接。我确实使用findByTag来重新引用已创建的片段。但由于某种原因,它仍然无法正常工作...... – 372