片段是什么我在托管和显示? [英] What Fragments am I hosting and displaying?
问题描述
有没有办法知道哪些片段
当前的在一个给定的&LT显示
的;片断> 的活动的容器
没有保留通过 onAttachFragment
回调?
Is there a way to know which Fragment
is currently displayed in a given <fragment>
container of an Activity
without keeping track of all the changes via the onAttachFragment
callback?
它甚至有可能知道哪些片段显示时,当用户presses返回键片段交易能够发生?在后一种情况下,即当一个片段
重新显示由于回来, onAttach
不叫。
Is it even possible to know which fragments are displayed when fragment transactions can take place when the user presses the back key? In this latter case, i.e. when a Fragment
is re-displayed due to a back, the onAttach
is not called.
推荐答案
当你添加/替换片段容器每一次,使用标签吧:
Each time when you add/replace fragment to the container, use tag for it:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.container, fragment, tag).commit();
然后就可以找出片段是当前可见或不可见:
then you can find out the fragment is current visible or not:
Fragment fg = getFragmentManger().findFragmentByTag(tag);
if(fg.isVisible())
//fg is the current visible fragment
希望这有助于!
Hope this help!
这篇关于片段是什么我在托管和显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!