My fragments in viewpager tab dont refresh

后端 未结 6 1984
我寻月下人不归
我寻月下人不归 2020-12-12 22:54

i got viewpager with 4 tabs .. in each tab there is a fragment. my first tab is a fragment with a form (for example Users) after i click save, the data is inserted in the da

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 23:41

    In my experience, ViewPagers keep:

    • Your current tab
    • & 1 tab either side in memory

    So when you switch between 1 and 2, nothing is happening under the hood - it's like they are simply being hidden / shown.

    But when you navigate to tab 4, tabs 1 & 2 are destroyed (onDestroy() called), so when you navigate back to either of them, they are being re-created fresh (onCreate() called).

    As psykhi suggests, you could setOffScreenPageLimit() to 0, so that each fragment is created every time you navigate to it.

    If you were interested in keeping the other pages in memory for performance purposes, as they were designed with this is mind, you could use a messaging/event publishing system to send a message from tab 1 to tab 2 telling it to update when you submit the form on tab 1.

提交回复
热议问题