From 2b843195e497a6fd8dc8e10ce3438fd4b8806652 Mon Sep 17 00:00:00 2001 From: Martin Yankov Date: Mon, 29 Jan 2018 17:25:36 +0200 Subject: [PATCH] fix-next(android-tabview): handle tab fragments attach/detach properly (#5333) --- tns-core-modules/ui/frame/frame.android.ts | 1 - .../ui/tab-view/tab-view.android.ts | 62 ++++++++++++------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/tns-core-modules/ui/frame/frame.android.ts b/tns-core-modules/ui/frame/frame.android.ts index 094a9fbfc..0c394dff7 100644 --- a/tns-core-modules/ui/frame/frame.android.ts +++ b/tns-core-modules/ui/frame/frame.android.ts @@ -740,7 +740,6 @@ class FragmentCallbacksImplementation implements AndroidFragmentCallbacks { @profile public onStop(fragment: android.app.Fragment, superFunc: Function): void { superFunc.call(fragment); - this.entry.resolvedPage.callUnloaded(); } @profile diff --git a/tns-core-modules/ui/tab-view/tab-view.android.ts b/tns-core-modules/ui/tab-view/tab-view.android.ts index 9572b75c6..bf13a5101 100644 --- a/tns-core-modules/ui/tab-view/tab-view.android.ts +++ b/tns-core-modules/ui/tab-view/tab-view.android.ts @@ -78,7 +78,11 @@ function initializeNativeClasses() { } } + const POSITION_UNCHANGED = -1; + const POSITION_NONE = -2; + class FragmentPagerAdapter extends android.support.v4.view.PagerAdapter { + public items: Array; private mCurTransaction: android.app.FragmentTransaction; private mCurrentPrimaryItem: android.app.Fragment; @@ -88,12 +92,12 @@ function initializeNativeClasses() { } getCount() { - const items = this.owner.items; + const items = this.items; return items ? items.length : 0; } getPageTitle(index: number) { - const items = this.owner.items; + const items = this.items; if (index < 0 || index >= items.length) { return ""; } @@ -129,15 +133,19 @@ function initializeNativeClasses() { fragment.setUserVisibleHint(false); } - const items = this.owner.items; - const item = items ? items[position] : null; - if (item) { - item.canBeLoaded = true; + const tabItems = this.owner.items; + const tabItem = tabItems ? tabItems[position] : null; + if (tabItem) { + tabItem.canBeLoaded = true; } return fragment; } + getItemPosition(object: java.lang.Object): number { + return this.items ? POSITION_UNCHANGED : POSITION_NONE; + } + destroyItem(container: android.view.ViewGroup, position: number, object: java.lang.Object): void { if (!this.mCurTransaction) { const fragmentManager = this.owner._getFragmentManager(); @@ -147,10 +155,10 @@ function initializeNativeClasses() { const fragment: android.app.Fragment = object; this.mCurTransaction.detach(fragment); - const items = this.owner.items; - const item = items ? items[position] : null; - if (item) { - item.canBeLoaded = false; + const tabItems = this.owner.items; + const tabItem = tabItems ? tabItems[position] : null; + if (tabItem) { + tabItem.canBeLoaded = false; } } @@ -169,13 +177,13 @@ function initializeNativeClasses() { this.mCurrentPrimaryItem = fragment; this.owner.selectedIndex = position; - - const tab = this.owner; - const items = tab.items; - const newItem = items ? items[position] : null; - if (newItem && tab.isLoaded) { - newItem.loadView(newItem.view); - } + } + + const tab = this.owner; + const tabItems = tab.items; + const newTabItem = tabItems ? tabItems[position] : null; + if (newTabItem && tab.isLoaded) { + newTabItem.loadView(newTabItem.view); } } @@ -253,6 +261,10 @@ export class TabViewItem extends TabViewItemBase { } } + public onLoaded(): void { + super.onLoaded(); + } + public resetNativeView(): void { super.resetNativeView(); if (this.nativeViewProtected) { @@ -409,6 +421,18 @@ export class TabView extends TabViewBase { } } + public onLoaded(): void { + super.onLoaded(); + + this.setAdapterItems(this.items); + } + + public onUnloaded(): void { + super.onUnloaded(); + + this.setAdapterItems(null); + } + public disposeNativeView() { (this._pagerAdapter).items = null; this._tabLayout.setItems(null, null); @@ -448,10 +472,6 @@ export class TabView extends TabViewBase { tabItems.push(tabItemSpec); }); - // // TODO: optimize by reusing the adapter and calling setAdapter(null) then the same adapter. - // this._pagerAdapter = new PagerAdapter(this, items); - // this._viewPager.setAdapter(this._pagerAdapter); - const tabLayout = this._tabLayout; tabLayout.setItems(tabItems, this._viewPager); items.forEach((item, i, arr) => {