Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when Fragment becomes invisible

I am using ViewPager with PagerSlidingTabStrip , i have 3 fragments , every fragment has its own custom listeners. In every fragment I overrided Destroy, Pause methods, but unfortunately when I move from one fragment to other I have to remove listeners of One fragment but none of the above methods are called as fragments remain in memory. These methods are only called when i am moving to another Activity. So can any body tell me how can I know if a fragment is going to made invisible so that I can remove Listeners, otherwise these listeners are going to disturb that data for all my fragments.

like image 778
Ali Avatar asked Mar 07 '14 21:03

Ali


People also ask

How do you know when a fragment becomes visible?

fragment:fragment:1.1. 0 you can just use onPause and onResume callbacks to determine which fragment is currently visible for the user. onResume callback is called when fragment became visible and onPause when it stops to be visible.

How do you know if a fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.

Are fragments visible?

For example, when the Activity receives onPause() , it triggers a Fragment onPause() for each Fragment in the Activity . Fragment is added and its layout is inflated. Fragment is active and visible.

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .


3 Answers

I use this in my fragment, this is called twice. 1) when you go out of screen 2) when fragment is first created. TO solve check this link

@Override
    public void setUserVisibleHint(boolean visible){
        super.setUserVisibleHint(visible);
        if (!visible){
            //when fragment becomes invisible to user,do what you want...
        }
}
like image 188
Jemshit Iskenderov Avatar answered Oct 31 '22 20:10

Jemshit Iskenderov


You should be using onPause() and onResume() as onDestroy() only gets called when the fragment is removed from memory, not when it stops being run.

Also, in a paging setup, neighbouring fragments will continue to run. One on either side of current fragment.

For the visibility check you could check this thread:

How to determine when Fragment becomes visible in ViewPager

like image 44
Cory Roy Avatar answered Oct 31 '22 20:10

Cory Roy


With ViewPager you should better override Fragment.setUserVisibleHint() Fragment.onHiddenChanged() and react on visible/hidden state change. You still need to implement onPause() / onResume(). I explained it in this answer in more details.

like image 35
sergej shafarenka Avatar answered Oct 31 '22 20:10

sergej shafarenka