@@ -206,7 +206,8 @@ function initializeNativeClasses() {
206
206
this . mCurTransaction = fragmentManager ?. beginTransaction ( ) ;
207
207
}
208
208
209
- const index = this . owner . fragments . indexOf ( fragment ) ;
209
+ // detached fragments are still attached to the fragment manager
210
+ // const index = this.owner.fragments.indexOf(fragment);
210
211
// if (index !== -1) {
211
212
// this.owner.fragments.splice(index, 1);
212
213
// }
@@ -620,22 +621,26 @@ export class Tabs extends TabsBase {
620
621
}
621
622
622
623
private disposeCurrentFragments ( ) : void {
623
- let fragmentManager : androidx . fragment . app . FragmentManager ;
624
+ // we need to use this because the destroyItem only detaches the item
625
+ // here we clean up all fragments, even ones that were detached to another manager, which may happen on suspend/resume
626
+ // alternative: actually remove the fragment on destroyItem
627
+ const transactionMap = new Map < androidx . fragment . app . FragmentManager , androidx . fragment . app . FragmentTransaction > ( ) ;
624
628
for ( const fragment of this . fragments ) {
625
- fragmentManager = this . _getParentFragmentManagerFromFragment ( fragment ) ;
626
- if ( fragmentManager ) {
627
- break ;
629
+ const fragmentManager = this . _getParentFragmentManagerFromFragment ( fragment ) ;
630
+ if ( ! fragmentManager || fragmentManager . isDestroyed ( ) ) {
631
+ continue ;
628
632
}
629
- }
630
- if ( fragmentManager ) {
631
- const transaction = fragmentManager . beginTransaction ( ) ;
632
-
633
- const fragments = this . fragments ;
634
- for ( let i = 0 ; i < fragments . length ; i ++ ) {
635
- transaction . remove ( fragments [ i ] ) ;
633
+ if ( ! transactionMap . has ( fragmentManager ) ) {
634
+ transactionMap . set ( fragmentManager , fragmentManager . beginTransaction ( ) ) ;
636
635
}
636
+ const transaction = transactionMap . get ( fragmentManager ) ;
637
+
638
+ transaction . remove ( fragment ) ;
639
+ }
640
+ for ( const transaction of transactionMap . values ( ) ) {
637
641
transaction . commitNowAllowingStateLoss ( ) ;
638
642
}
643
+ transactionMap . clear ( ) ; // let's avoid memory leaks
639
644
this . fragments = [ ] ;
640
645
}
641
646
0 commit comments