Fragment onHiddenChanged not called

mikepenz picture mikepenz · Mar 6, 2012 · Viewed 16.3k times · Source

I recently added Fragments to my applications. For a new application i'll need to get notified as soon as my fragment is shown. So i can do some calculations as soon as my fragment is shown again.

My Fragment is used with a TabIndicator and it's only one FragmentClass which is used a few times.

Here's the normal standard override class:

@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);
}

Answer

AndreyNik picture AndreyNik · Aug 30, 2013

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.