1- import { Application , CoercibleProperty , Color , Enums , Font , Frame , ImageSource , Property , Utils , getTransformedText , isIOS } from '@nativescript/core' ;
1+ import { Application , CoercibleProperty , Color , Enums , Font , Frame , ImageSource , Property , Utils , getTransformedText , isIOS , View } from '@nativescript/core' ;
22import { TabStrip } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-strip' ;
33import { TabStripItem } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-strip-item' ;
44import { TabContentItem } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-content-item' ;
@@ -14,13 +14,15 @@ const DEFAULT_ELEVATION = 4;
1414
1515const TABID = '_tabId' ;
1616const INDEX = '_index' ;
17+ const ownerSymbol = Symbol ( '_owner' ) ;
1718
1819type PagerAdapter = new ( owner : Tabs ) => androidx . viewpager . widget . PagerAdapter ;
1920
2021// eslint-disable-next-line no-redeclare
2122let PagerAdapter : PagerAdapter ;
2223let TabsBar : any ;
2324let appResources : android . content . res . Resources ;
25+ let AttachStateChangeListener : any ;
2426
2527function makeFragmentName ( viewId : number , id : number ) : string {
2628 return 'android:viewpager:' + viewId + ':' + id ;
@@ -326,8 +328,33 @@ function initializeNativeClasses() {
326328 }
327329 }
328330
331+ @NativeClass
332+ @Interfaces ( [ android . view . View . OnAttachStateChangeListener ] )
333+ class AttachListener extends java . lang . Object implements android . view . View . OnAttachStateChangeListener {
334+ constructor ( ) {
335+ super ( ) ;
336+
337+ return global . __native ( this ) ;
338+ }
339+
340+ onViewAttachedToWindow ( view : android . view . View ) : void {
341+ const owner : View = view [ ownerSymbol ] ;
342+ if ( owner ) {
343+ owner . _onAttachedToWindow ( ) ;
344+ }
345+ }
346+
347+ onViewDetachedFromWindow ( view : android . view . View ) : void {
348+ const owner : View = view [ ownerSymbol ] ;
349+ if ( owner ) {
350+ owner . _onDetachedFromWindow ( ) ;
351+ }
352+ }
353+ }
354+
329355 PagerAdapter = FragmentPagerAdapter ;
330356 TabsBar = TabsBarImplementation ;
357+ AttachStateChangeListener = new AttachListener ( ) ;
331358 appResources = Application . android . context . getResources ( ) ;
332359}
333360
@@ -374,6 +401,7 @@ export class Tabs extends TabsBase {
374401 private _selectedItemColor : Color ;
375402 private _unSelectedItemColor : Color ;
376403 fragments : androidx . fragment . app . Fragment [ ] = [ ] ;
404+ private _attachedToWindow = false ;
377405
378406 constructor ( ) {
379407 super ( ) ;
@@ -457,13 +485,36 @@ export class Tabs extends TabsBase {
457485 const nativeView : any = this . nativeViewProtected ;
458486 this . _tabsBar = nativeView . tabsBar ;
459487
488+ // nativeView.addOnAttachStateChangeListener(AttachStateChangeListener);
489+ nativeView [ ownerSymbol ] = this ;
490+
460491 const viewPager = nativeView . viewPager ;
461492 viewPager . setId ( this . _androidViewId ) ;
462493 this . _viewPager = viewPager ;
463494 this . _pagerAdapter = viewPager . adapter ;
464495 ( this . _pagerAdapter as any ) . owner = this ;
465496 }
466497
498+ _onAttachedToWindow ( ) : void {
499+ super . _onAttachedToWindow ( ) ;
500+
501+ // _onAttachedToWindow called from OS again after it was detach
502+ // TODO: Consider testing and removing it when update to androidx.fragment:1.2.0
503+ if ( this . _manager && this . _manager . isDestroyed ( ) ) {
504+ return ;
505+ }
506+
507+ this . _attachedToWindow = true ;
508+ this . _viewPager . setCurrentItem ( this . selectedIndex , false ) ;
509+ }
510+
511+ _onDetachedFromWindow ( ) : void {
512+ super . _onDetachedFromWindow ( ) ;
513+ this . disposeCurrentFragments ( ) ;
514+
515+ this . _attachedToWindow = false ;
516+ }
517+
467518 public _loadUnloadTabItems ( newIndex : number ) {
468519 const items = this . items ;
469520 if ( ! items ) {
@@ -538,6 +589,9 @@ export class Tabs extends TabsBase {
538589 ( this . _pagerAdapter as any ) . owner = null ;
539590 this . _pagerAdapter = null ;
540591
592+ this . nativeViewProtected . removeOnAttachStateChangeListener ( AttachStateChangeListener ) ;
593+ this . nativeViewProtected [ ownerSymbol ] = null ;
594+
541595 this . _tabsBar = null ;
542596 this . _viewPager = null ;
543597 super . disposeNativeView ( ) ;
0 commit comments