@@ -103,7 +103,7 @@ function initializeNativeClasses() {
103103
104104 // Get view as bitmap and set it as background. This is workaround for the disapearing nested fragments.
105105 // TODO: Consider removing it when update to androidx.fragment:1.2.0
106- if ( hasRemovingParent && this . owner . selectedIndex === this . index ) {
106+ if ( hasRemovingParent && this . owner . selectedIndex === this . index && this . owner . nativeViewProtected ) {
107107 this . backgroundBitmap = this . loadBitmapFromView ( this . owner . nativeViewProtected ) ;
108108 }
109109
@@ -200,18 +200,17 @@ function initializeNativeClasses() {
200200 }
201201
202202 destroyItem ( container : android . view . ViewGroup , position : number , object : java . lang . Object ) : void {
203+ const fragment : androidx . fragment . app . Fragment = object as androidx . fragment . app . Fragment ;
203204 if ( ! this . mCurTransaction ) {
204- const fragmentManager = this . owner . _getFragmentManager ( ) ;
205- this . mCurTransaction = fragmentManager . beginTransaction ( ) ;
205+ const fragmentManager : androidx . fragment . app . FragmentManager = this . owner . _getParentFragmentManagerFromFragment ( fragment ) ;
206+ this . mCurTransaction = fragmentManager ? .beginTransaction ( ) ;
206207 }
207208
208- const fragment : androidx . fragment . app . Fragment = object as androidx . fragment . app . Fragment ;
209-
210209 const index = this . owner . fragments . indexOf ( fragment ) ;
211210 // if (index !== -1) {
212211 // this.owner.fragments.splice(index, 1);
213212 // }
214- this . mCurTransaction . detach ( fragment ) ;
213+ this . mCurTransaction ? .detach ( fragment ) ;
215214
216215 if ( this . mCurrentPrimaryItem === fragment ) {
217216 this . mCurrentPrimaryItem = null ;
@@ -479,7 +478,7 @@ export class Tabs extends TabsBase {
479478 return nativeView ;
480479 }
481480 onSelectedIndexChanged ( oldIndex : number , newIndex : number ) {
482- const tabBarImplementation = ( this . _tabsBar as unknown ) as PositionChanger ;
481+ const tabBarImplementation = this . _tabsBar as unknown as PositionChanger ;
483482 if ( tabBarImplementation ) {
484483 tabBarImplementation . onSelectedPositionChange ( oldIndex , newIndex ) ;
485484 }
@@ -621,14 +620,22 @@ export class Tabs extends TabsBase {
621620 }
622621
623622 private disposeCurrentFragments ( ) : void {
624- const fragmentManager = this . _getFragmentManager ( ) ;
625- const transaction = fragmentManager . beginTransaction ( ) ;
623+ let fragmentManager : androidx . fragment . app . FragmentManager ;
624+ for ( const fragment of this . fragments ) {
625+ fragmentManager = this . _getParentFragmentManagerFromFragment ( fragment ) ;
626+ if ( fragmentManager ) {
627+ break ;
628+ }
629+ }
630+ if ( fragmentManager ) {
631+ const transaction = fragmentManager . beginTransaction ( ) ;
626632
627- const fragments = this . fragments ;
628- for ( let i = 0 ; i < fragments . length ; i ++ ) {
629- transaction . remove ( fragments [ i ] ) ;
633+ const fragments = this . fragments ;
634+ for ( let i = 0 ; i < fragments . length ; i ++ ) {
635+ transaction . remove ( fragments [ i ] ) ;
636+ }
637+ transaction . commitNowAllowingStateLoss ( ) ;
630638 }
631- transaction . commitNowAllowingStateLoss ( ) ;
632639 this . fragments = [ ] ;
633640 }
634641
0 commit comments