简体   繁体   中英

ViewPager, when do I save state of my fragment

I have a fragment that loads data in the onResume/onCreate method and saves data in the onPause method. When I place this fragment in a viewpager it is initialized when it is the frament 1 left or 1 right to the current fragment shown on the screen.

During this time onResume is called and the fragment data is loaded. Which is fine.

However when the fragment is visible and the user swipes to another fragment no life cycle methods are called that I can find (onPause/ onStop/ onDetatch .. etc). The onPause/ onStop are only called when the fragment is 2 fragments left or 2 fragments right to the current one shown on the screen.

I would like to know how other people handle this, when do you save state in a Fragment which is shown in a ViewPager?

you can call this method to understand which fragment you are in

@Override
    public void setMenuVisibility(final boolean visible) {
        super.setMenuVisibility(visible);
        if((visible)  )
        {
             //do something here    
        }

      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM