简体   繁体   中英

how ViewPager2 handle fragments

In my app I have a ViewPager2, the fragment inside this ViewPager have some code on onViewCreated. I have 2 question:

  1. If I update the dataset and then return to old one the onViewCreated is not called, because of fragment cache, how can I intercept the re-show? I am ok with cache, bui I want restore the initial focus of the fragment when re-showed.
  2. How can I intercept the hiding from the fragment? My fragment show a dialog, then an external event can change the data set, I want in this case dismiss the dialog.

Are 2 days that I am trying to fix those 2 issues.

EDIT: I solved the 2 observing the changing of data set.

Data sharing by ViewModel looks like a good solution for both cases. Fragments will rely on data change in shared ViewModel (depends on the structure of your screen it can be data sharing with host activity or parent fragment). If it's not what you are looking for, provide more details.

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