2013-08-26 33 views
4

喜的朋友,我有两个的FrameLayout在movies.xml即容器detail_screen。在容器将增加movies.xml其中包含列表视图,并在detail_screen有一个名为movie_details.xml。现在想扩张的ListView在detail_screen中以编程方式检查已经是片段呈现或不。如果提交只是想删除该片段。我做了follwing和它的工作正常。检查片段是在framelayout中呈现还是不在android中?

if (getSupportFragmentManager().findFragmentByTag("MyFragment") != null) { 
     getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentByTag("MyFragment")).commit(); 
} 

但没有任何其他的方式来寻找是否片段通过编程方式在Android中呈现的或不框架布局.Thanks提前

+0

不使用findFragmentByTag()/编号 – krishh

+0

你想知道,如果一个片段存在,而无需将它或它的容器的引用?有'getSupportFragmentManager()。getFragments()'它检索该管理器的片段列表。 – kassim

+0

@kmg我想,以检查是否已经在我的FrameLayout片段呈现只想删除片段添加新的片段,否则相同的片段将很好,如果你在使用'getSupportFragmentManager添加不止一次权 – krishh

回答

7

我添加任何碎片details_screen如果已经呈现片段,然后弹出,然后将添加新的片段只是检查之前成立的溶液,作为follows.i.e。

if (getSupportFragmentManager().findFragmentById(R.id.detail_screen) != null) { 

     getSupportFragmentManager().popBackStack(); 

      } //to do add fragment 

希望它能工作。

6

使用findFragmentByTag()findFragmentById()方法来找到它。如果返回的实例不为null,则该片段存在。此外,您可以使用Fragment.isInLayout()方法来检查是否片段布局使用<fragment>标签来定义或编程方式添加。

+0

感谢您的快速回复 – krishh

相关问题