FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(android): BottomNavigation fragment child already has a parent by NathanWalker · Pull Request #9148 · NativeScript/NativeScript · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
86 changes: 63 additions & 23 deletions packages/core/ui/bottom-navigation/index.android.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export class BottomNavigation extends TabNavigationBase {
const fragmentToDetach = this._currentFragment;
if (fragmentToDetach) {
this.destroyItem((<any>fragmentToDetach).index, fragmentToDetach);
this.commitCurrentTransaction();
this.removeFragment(fragmentToDetach);
}
}

Expand All @@ -454,28 +454,42 @@ export class BottomNavigation extends TabNavigationBase {

private disposeTabFragments(): void {
const fragmentManager = this._getFragmentManager();
const transaction = fragmentManager.beginTransaction();
const fragments = fragmentManager.getFragments().toArray();
for (let i = 0; i < fragments.length; i++) {
transaction.remove(fragments[i]);
this.removeFragment(fragments[i]);
}

transaction.commitNowAllowingStateLoss();
}

private get currentTransaction(): androidx.fragment.app.FragmentTransaction {
if (!this._currentTransaction) {
const fragmentManager = this._getFragmentManager();
this._currentTransaction = fragmentManager.beginTransaction();
}

return this._currentTransaction;
}

private commitCurrentTransaction(): void {
if (this._currentTransaction) {
this._currentTransaction.commitNowAllowingStateLoss();
this._currentTransaction = null;
private attachFragment(fragment: androidx.fragment.app.Fragment, id?: number, name?: string): void {
const fragmentManager = this._getFragmentManager();
if (fragment) {
if (fragment.isAdded() || fragment.isRemoving()) {
// ignore
} else {
const fragmentExitTransition = fragment.getExitTransition();
if (fragmentExitTransition && fragmentExitTransition instanceof org.nativescript.widgets.CustomTransition) {
fragmentExitTransition.setResetOnTransitionEnd(true);
}
if (fragmentManager) {
if (!fragmentManager.isDestroyed()) {
try {
if (fragmentManager.isStateSaved()) {
if (id && name) {
fragmentManager.beginTransaction().add(id, fragment, name).commitNowAllowingStateLoss();
} else {
fragmentManager.beginTransaction().attach(fragment).commitNowAllowingStateLoss();
}
} else {
if (id && name) {
fragmentManager.beginTransaction().add(id, fragment, name).commitNow();
} else {
fragmentManager.beginTransaction().attach(fragment).commitNow();
}
}
} catch (e) {}
}
}
}
}
}

Expand All @@ -495,8 +509,6 @@ export class BottomNavigation extends TabNavigationBase {

const fragment = this.instantiateItem(this._contentView, index);
this.setPrimaryItem(index, fragment);

this.commitCurrentTransaction();
}

private instantiateItem(container: android.view.ViewGroup, position: number): androidx.fragment.app.Fragment {
Expand All @@ -505,10 +517,10 @@ export class BottomNavigation extends TabNavigationBase {
const fragmentManager = this._getFragmentManager();
let fragment: androidx.fragment.app.Fragment = fragmentManager.findFragmentByTag(name);
if (fragment != null) {
this.currentTransaction.attach(fragment);
this.attachFragment(fragment);
} else {
fragment = TabFragment.newInstance(this._domId, position);
this.currentTransaction.add(container.getId(), fragment, name);
this.attachFragment(fragment, container.getId(), name);
}

if (fragment !== this._currentFragment) {
Expand Down Expand Up @@ -545,7 +557,7 @@ export class BottomNavigation extends TabNavigationBase {

private destroyItem(position: number, fragment: androidx.fragment.app.Fragment): void {
if (fragment) {
this.currentTransaction.detach(fragment);
this.removeFragment(fragment);
if (this._currentFragment === fragment) {
this._currentFragment = null;
}
Expand All @@ -555,6 +567,34 @@ export class BottomNavigation extends TabNavigationBase {
this.items[position].canBeLoaded = false;
}
}
private removeFragment(fragment: androidx.fragment.app.Fragment, fragmentManager?: any) {
if (!fragmentManager) {
fragmentManager = this._getFragmentManager();
}
if (fragment) {
if (!fragment.isAdded() || fragment.isRemoving()) {
// ignore
return;
} else {
const fragmentExitTransition = fragment.getExitTransition();
if (fragmentExitTransition && fragmentExitTransition instanceof org.nativescript.widgets.CustomTransition) {
fragmentExitTransition.setResetOnTransitionEnd(true);
}
if (fragment && fragment.isAdded() && !fragment.isRemoving()) {
const pfm = (<any>fragment).getParentFragmentManager ? (<any>fragment).getParentFragmentManager() : null;
if (pfm && !pfm.isDestroyed()) {
try {
if (pfm.isStateSaved()) {
pfm.beginTransaction().remove(fragment).commitNowAllowingStateLoss();
} else {
pfm.beginTransaction().remove(fragment).commitNow();
}
} catch (e) {}
}
}
}
}
}

private setTabStripItems(items: Array<TabStripItem>) {
if (!this.tabStrip || !items) {
Expand Down

Back | FazBrowse Home | New Git URL