Fragment onHiddenChanged not called

五迷三道 提交于 2019-12-04 22:47:40

Still looking for an answer? onHiddenChanged doesn't get called the first time an fragment is shown. Only when it changes state.

From the documentation:

Called when the hidden state (as returned by isHidden()) of the fragment has changed. Fragments start out not hidden; this will be called whenever the fragment changes state from that.

AndreyNik

I had same problem.

I used standart guideline practic work with fragment (Building a Flexible UI). I have two fragment (ListItemsFragment and InfoItemFragment). When used normal screen size, I replace ListItemsFragment at InfoItemFragment and the method onHiddenChanged doesn't call automatic.

FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.replace(R.id.container_fragment, new InfoItemFragment(), "tag_fr_infoItem");
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();

I think we must called in hide method FragmentTransaction. For example:

    ListItemsFragment  mListItemsFragment;
    FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
    mFragmentTransaction.replace(R.id.container_fragment, new InfoItemFragment(), "tag_fr_infoItem");
    if (mListItemsFragment != null) {
        mFragmentTransaction.hide(mListItemsFragment);
    }
    mFragmentTransaction.addToBackStack(null);
    mFragmentTransaction.commit();

And now the method onHiddenChanged work fine. When user click back button mListItemsFragment again show and method onHiddenChanged called automatic.

In documentation said: this will be called whenever the fragment changes state from that

I think we must manual change value then method will be called.

You can use setUserVisibleHint method to solve some similar problem. Hope it can help you.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // Do some your work
    } else {
        // Do your Work
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!