| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -428,7 +428,7 @@ export class BottomNavigation extends TabNavigationBase { | |||
| 428 | 428 | const fragmentToDetach = this._currentFragment; | |
| 429 | 429 | if (fragmentToDetach) { | |
| 430 | 430 | this.destroyItem((<any>fragmentToDetach).index, fragmentToDetach); | |
| 431 | - this.commitCurrentTransaction(); | ||
| 431 | + this.removeFragment(fragmentToDetach); | ||
| 432 | 432 | } | |
| 433 | 433 | } | |
| 434 | 434 | ||
@@ -454,28 +454,42 @@ export class BottomNavigation extends TabNavigationBase { | |||
| 454 | 454 | ||
| 455 | 455 | private disposeTabFragments(): void { | |
| 456 | 456 | const fragmentManager = this._getFragmentManager(); | |
| 457 | - const transaction = fragmentManager.beginTransaction(); | ||
| 458 | 457 | const fragments = fragmentManager.getFragments().toArray(); | |
| 459 | 458 | for (let i = 0; i < fragments.length; i++) { | |
| 460 | - transaction.remove(fragments[i]); | ||
| 459 | + this.removeFragment(fragments[i]); | ||
| 461 | 460 | } | |
| 462 | - | ||
| 463 | - transaction.commitNowAllowingStateLoss(); | ||
| 464 | - } | ||
| 465 | - | ||
| 466 | - private get currentTransaction(): androidx.fragment.app.FragmentTransaction { | ||
| 467 | - if (!this._currentTransaction) { | ||
| 468 | - const fragmentManager = this._getFragmentManager(); | ||
| 469 | - this._currentTransaction = fragmentManager.beginTransaction(); | ||
| 470 | - } | ||
| 471 | - | ||
| 472 | - return this._currentTransaction; | ||
| 473 | 461 | } | |
| 474 | 462 | ||
| 475 | - private commitCurrentTransaction(): void { | ||
| 476 | - if (this._currentTransaction) { | ||
| 477 | - this._currentTransaction.commitNowAllowingStateLoss(); | ||
| 478 | - this._currentTransaction = null; | ||
| 463 | + private attachFragment(fragment: androidx.fragment.app.Fragment, id?: number, name?: string): void { | ||
| 464 | + const fragmentManager = this._getFragmentManager(); | ||
| 465 | + if (fragment) { | ||
| 466 | + if (fragment.isAdded() || fragment.isRemoving()) { | ||
| 467 | + // ignore | ||
| 468 | + } else { | ||
| 469 | + const fragmentExitTransition = fragment.getExitTransition(); | ||
| 470 | + if (fragmentExitTransition && fragmentExitTransition instanceof org.nativescript.widgets.CustomTransition) { | ||
| 471 | + fragmentExitTransition.setResetOnTransitionEnd(true); | ||
| 472 | + } | ||
| 473 | + if (fragmentManager) { | ||
| 474 | + if (!fragmentManager.isDestroyed()) { | ||
| 475 | + try { | ||
| 476 | + if (fragmentManager.isStateSaved()) { | ||
| 477 | + if (id && name) { | ||
| 478 | + fragmentManager.beginTransaction().add(id, fragment, name).commitNowAllowingStateLoss(); | ||
| 479 | + } else { | ||
| 480 | + fragmentManager.beginTransaction().attach(fragment).commitNowAllowingStateLoss(); | ||
| 481 | + } | ||
| 482 | + } else { | ||
| 483 | + if (id && name) { | ||
| 484 | + fragmentManager.beginTransaction().add(id, fragment, name).commitNow(); | ||
| 485 | + } else { | ||
| 486 | + fragmentManager.beginTransaction().attach(fragment).commitNow(); | ||
| 487 | + } | ||
| 488 | + } | ||
| 489 | + } catch (e) {} | ||
| 490 | + } | ||
| 491 | + } | ||
| 492 | + } | ||
| 479 | 493 | } | |
| 480 | 494 | } | |
| 481 | 495 | ||
@@ -495,8 +509,6 @@ export class BottomNavigation extends TabNavigationBase { | |||
| 495 | 509 | ||
| 496 | 510 | const fragment = this.instantiateItem(this._contentView, index); | |
| 497 | 511 | this.setPrimaryItem(index, fragment); | |
| 498 | - | ||
| 499 | - this.commitCurrentTransaction(); | ||
| 500 | 512 | } | |
| 501 | 513 | ||
| 502 | 514 | private instantiateItem(container: android.view.ViewGroup, position: number): androidx.fragment.app.Fragment { | |
@@ -505,10 +517,10 @@ export class BottomNavigation extends TabNavigationBase { | |||
| 505 | 517 | const fragmentManager = this._getFragmentManager(); | |
| 506 | 518 | let fragment: androidx.fragment.app.Fragment = fragmentManager.findFragmentByTag(name); | |
| 507 | 519 | if (fragment != null) { | |
| 508 | - this.currentTransaction.attach(fragment); | ||
| 520 | + this.attachFragment(fragment); | ||
| 509 | 521 | } else { | |
| 510 | 522 | fragment = TabFragment.newInstance(this._domId, position); | |
| 511 | - this.currentTransaction.add(container.getId(), fragment, name); | ||
| 523 | + this.attachFragment(fragment, container.getId(), name); | ||
| 512 | 524 | } | |
| 513 | 525 | ||
| 514 | 526 | if (fragment !== this._currentFragment) { | |
@@ -545,7 +557,7 @@ export class BottomNavigation extends TabNavigationBase { | |||
| 545 | 557 | ||
| 546 | 558 | private destroyItem(position: number, fragment: androidx.fragment.app.Fragment): void { | |
| 547 | 559 | if (fragment) { | |
| 548 | - this.currentTransaction.detach(fragment); | ||
| 560 | + this.removeFragment(fragment); | ||
| 549 | 561 | if (this._currentFragment === fragment) { | |
| 550 | 562 | this._currentFragment = null; | |
| 551 | 563 | } | |
@@ -555,6 +567,34 @@ export class BottomNavigation extends TabNavigationBase { | |||
| 555 | 567 | this.items[position].canBeLoaded = false; | |
| 556 | 568 | } | |
| 557 | 569 | } | |
| 570 | + private removeFragment(fragment: androidx.fragment.app.Fragment, fragmentManager?: any) { | ||
| 571 | + if (!fragmentManager) { | ||
| 572 | + fragmentManager = this._getFragmentManager(); | ||
| 573 | + } | ||
| 574 | + if (fragment) { | ||
| 575 | + if (!fragment.isAdded() || fragment.isRemoving()) { | ||
| 576 | + // ignore | ||
| 577 | + return; | ||
| 578 | + } else { | ||
| 579 | + const fragmentExitTransition = fragment.getExitTransition(); | ||
| 580 | + if (fragmentExitTransition && fragmentExitTransition instanceof org.nativescript.widgets.CustomTransition) { | ||
| 581 | + fragmentExitTransition.setResetOnTransitionEnd(true); | ||
| 582 | + } | ||
| 583 | + if (fragment && fragment.isAdded() && !fragment.isRemoving()) { | ||
| 584 | + const pfm = (<any>fragment).getParentFragmentManager ? (<any>fragment).getParentFragmentManager() : null; | ||
| 585 | + if (pfm && !pfm.isDestroyed()) { | ||
| 586 | + try { | ||
| 587 | + if (pfm.isStateSaved()) { | ||
| 588 | + pfm.beginTransaction().remove(fragment).commitNowAllowingStateLoss(); | ||
| 589 | + } else { | ||
| 590 | + pfm.beginTransaction().remove(fragment).commitNow(); | ||
| 591 | + } | ||
| 592 | + } catch (e) {} | ||
| 593 | + } | ||
| 594 | + } | ||
| 595 | + } | ||
| 596 | + } | ||
| 597 | + } | ||
| 558 | 598 | ||
| 559 | 599 | private setTabStripItems(items: Array<TabStripItem>) { | |
| 560 | 600 | if (!this.tabStrip || !items) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments