Skip to content

Commit e4f5e0f

Browse files
committed
fix: try to use the fragment's parent when possible
1 parent 8386631 commit e4f5e0f

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

src/bottom-navigation/index.android.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function initializeNativeClasses() {
113113

114114
// Get view as bitmap and set it as background. This is workaround for the disapearing nested fragments.
115115
// TODO: Consider removing it when update to androidx.fragment:1.2.0
116-
if (hasRemovingParent && this.owner.selectedIndex === this.index) {
116+
if (hasRemovingParent && this.owner.selectedIndex === this.index && this.owner.nativeViewProtected) {
117117
this.backgroundBitmap = this.loadBitmapFromView(this.owner.nativeViewProtected);
118118
}
119119

@@ -585,7 +585,11 @@ export class BottomNavigation extends TabNavigationBase {
585585
private hideFragment(fragment: androidx.fragment.app.Fragment, fragmentManager?: any) {
586586
if (!fragmentManager) {
587587
//@ts-ignore
588-
fragmentManager = this._getFragmentManager();
588+
fragmentManager = this._getParentFragmentManagerFromFragment(fragment);
589+
if(!fragmentManager) {
590+
// nothing to do
591+
return;
592+
}
589593
}
590594
if (fragment) {
591595
if (!fragment.isAdded() || fragment.isRemoving()) {
@@ -614,7 +618,11 @@ export class BottomNavigation extends TabNavigationBase {
614618
private showFragment(fragment: androidx.fragment.app.Fragment, fragmentManager?: any) {
615619
if (!fragmentManager) {
616620
//@ts-ignore
617-
fragmentManager = this._getFragmentManager();
621+
fragmentManager = this._getParentFragmentManagerFromFragment(fragment);
622+
if(!fragmentManager) {
623+
// nothing to do
624+
return;
625+
}
618626
}
619627
if (fragment) {
620628
if (!fragment.isAdded() || fragment.isRemoving()) {
@@ -643,7 +651,11 @@ export class BottomNavigation extends TabNavigationBase {
643651
private removeFragment(fragment: androidx.fragment.app.Fragment, fragmentManager?: any) {
644652
if (!fragmentManager) {
645653
//@ts-ignore
646-
fragmentManager = this._getFragmentManager();
654+
fragmentManager = this._getParentFragmentManagerFromFragment(fragment);
655+
if(!fragmentManager) {
656+
// nothing to do
657+
return;
658+
}
647659
}
648660
if (fragment) {
649661
if (!fragment.isAdded() || fragment.isRemoving()) {

src/core/tab-navigation-base/tab-navigation-base/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,21 @@ export class TabNavigationBase extends View implements TabNavigationBaseDefiniti
264264
// overridden by inheritors
265265
return null;
266266
}
267+
/**
268+
* Gets the parent fragment manager from a fragment to be used in destroying or hiding it.
269+
* @param fragment target fragment
270+
* @returns the parent fragment manager or null if none exists.
271+
*/
272+
public _getParentFragmentManagerFromFragment(fragment: androidx.fragment.app.Fragment) {
273+
if (!fragment) {
274+
return null;
275+
}
276+
try {
277+
return fragment.getParentFragmentManager();
278+
} catch (e) {
279+
return null;
280+
}
281+
}
267282
}
268283

269284
const MIN_ICON_SIZE = 24;

src/tabs/tabs.android.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)