Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment onHiddenChanged not called

Tags:

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); } 
like image 992
mikepenz Avatar asked Mar 06 '12 19:03

mikepenz


People also ask

What does the onCreateView () method return if a fragment doesn't have any UI?

These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.

What fragment method is called when the fragment is no longer visible to the user?

onStop() is normally called when fragment becomes invisible, but it can also be called later in time. Depending on your layout Android can call onStart() even, when your Fragment is not yet visible, but it belongs to a visible parent container.

What does the onCreateView () method return if a fragment doesn't have any UI Mcq?

onCreateView() : The system calls this callback when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

Which method fragment becomes active?

onStart()The onStart() method is called once the fragment gets visible. onResume()Fragment becomes active.


1 Answers

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.

like image 144
AndreyNik Avatar answered Oct 04 '22 23:10

AndreyNik