I've noticed that the default value of androidOffscreenTabLimit changed from 0 to 1 when setting androidTabsPosition to bottom(61bda9ecdfb97c09427640cdce1474f92ce1ef1b). What is the rationale for that? Is it because setting androidOffscreenTabLimit to 0 doesn't work? It seems that its minimum value is 1 in ViewPager[1].
So, what is the recommended approach to avoid preloading tabs as of now?
The documentation also needs to be updated if this is the case. (https://docs.nativescript.org/ui/ns-ui-widgets/tab-view)
@doffltmiw the preload option is only applicable when the tabView position is set to top (and that is why the line you are referring to is checking if the position is top). When the TabView is top (native Android behavior) we have a slide animation and preloading is making sense to assure that the transition from one fragment to another with animation will work as expected.
On the other hand, the bottom functionality is similar to the iOS one (no transition animation) and that's why we don't need explicit preloading when the tabs are positioned at the bottom.
I agree that this one needs to be described in the documentation in details so marking the issue as docs-needed
Issue moved to NativeScript/docs #1456 via ZenHub
@NickIliev No, the TabView with androidTabsPosition set to bottom preloads the tabs, it doesn't work like the iOS one. Setting androidOffscreenTabLimit to 0 doesn't help either.
I've created a playground demo here: https://play.nativescript.org/?template=play-js&id=tso5F2&v=2
Change tabs to see the console output. For example, selecting the tab4 from tab1 outputs "tab3 loaded" "tab4 loaded" "tab5 loaded".
@doffltmiw according to documentation (here)
Setting the Tabs at the bottom will disable the swipe navigation and the items preloading functionality.
The thing is that by design on Android at least one tab will be preloaded so setting 0 will not work (which is why this line prevents settings 0 when the tab position is bottom). By disabling the preload functionality the documentation section means that effectively you can not change the number of preloaded tabs when the position is bottom
@doffltmiw @NickIliev This was a breaking change we had to make in 5.0. In NS 4 we managed to set the preloading of the Android ViewPager to 0 with sort of a dirty workaround hack. Unfortunately, we found out that this caused too many sporadic crashes that were hard to track. We had to remove it. I agree, the docs need to be updated.
This also means that right now there is no effective way to avoid preloading tabs on Android. Is this breaking your application logic somehow?
@MartoYankov In most use cases for a bottom TabView interface, users wouldn't access tabs sequentially one after another, so preloading adjacent tabs will be a waste of resources. I also want the content of a tab reloaded from the server if it's not up to date as the user switches between them, but because of the preloading I can't do it in the page loaded event.
I should probably use the selectedIndexChanged event, but it gets tricky as this event can be fired even before the frame is constructed, so I guess something like this will work?
// attach this to each page in the tab items
export function onNavigatingTo(args: NavigatedData) {
const tabView = <TabView>topmost().parent.parent;
if (tabView.selectedIndex == 0 && args.isBackNavigation == false) {
loadData();
}
tabView.on("selectedIndexChanged", (args) => {
if (args.newIndex == 0) {
loadData();
} else {
releaseData();
}
})
}
@doffltmiw Yep, right now you will have to do something like this. We are aware of all the various problems with the component. Upgrading the TabView developer experience is on our immediate roadmap.
I was wondering why it androidOffscreenTabLimit didn麓t work when i had the tabs at the bottom. Welp, time to wait for a possible fix
Most helpful comment
@doffltmiw Yep, right now you will have to do something like this. We are aware of all the various problems with the component. Upgrading the TabView developer experience is on our immediate roadmap.