Skip to content

Commit 4c8932b

Browse files
committed
fix: trying to fix errors with bottom navigation and tabs
1 parent a50407a commit 4c8932b

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

src/bottom-navigation/index.android.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ export class BottomNavigation extends TabNavigationBase {
397397

398398
_onAttachedToWindow(): void {
399399
super._onAttachedToWindow();
400+
console.log('_onAttachedToWindow', new Error().stack)
400401

401402
// _onAttachedToWindow called from OS again after it was detach
402403
// TODO: Consider testing and removing it when update to androidx.fragment:1.2.0
@@ -410,7 +411,7 @@ export class BottomNavigation extends TabNavigationBase {
410411

411412
_onDetachedFromWindow(): void {
412413
super._onDetachedFromWindow();
413-
414+
this.disposeTabFragments();
414415
this._attachedToWindow = false;
415416
}
416417

@@ -448,11 +449,16 @@ export class BottomNavigation extends TabNavigationBase {
448449
}
449450

450451
private disposeTabFragments(): void {
452+
console.log('disposeTabFragsments');
451453
const fragments = this.fragments;
452454
for (let i = 0; i < fragments.length; i++) {
453455
this.removeFragment(fragments[i]);
454456
}
455-
this._currentFragment = null;
457+
// const items = this.items;
458+
// items.forEach((item, i) => {
459+
// item.unloadView(item.content);
460+
// });
461+
// this._currentFragment = null;
456462
this.fragments = [];
457463
}
458464
private attachFragment(fragment: androidx.fragment.app.Fragment, id?: number, name?: string): void {
@@ -493,7 +499,7 @@ export class BottomNavigation extends TabNavigationBase {
493499
public changeTab(index: number) {
494500
// index is -1 when there are no items
495501
// bot nav is not attached if you change the tab too early
496-
if (index === -1 || (this._currentFragment && index === this.selectedIndex) || !this._attachedToWindow) {
502+
if (index === -1 || !this._attachedToWindow) {
497503
return;
498504
}
499505

@@ -522,21 +528,23 @@ export class BottomNavigation extends TabNavigationBase {
522528
this.attachFragment(fragment, container.getId(), name);
523529
}
524530

525-
if (fragment !== this._currentFragment) {
526-
fragment.setMenuVisibility(false);
527-
fragment.setUserVisibleHint(false);
528-
}
531+
// if (fragment !== this._currentFragment) {
532+
// fragment.setMenuVisibility(false);
533+
// fragment.setUserVisibleHint(false);
534+
// }
529535

530536
return fragment;
531537
}
532538

533539
private setPrimaryItem(position: number, fragment: androidx.fragment.app.Fragment, force = false): void {
534540
if (fragment !== this._currentFragment || force) {
535541
if (this._currentFragment != null) {
542+
console.log('setPrimaryItem hiding curret', this.selectedIndex,this._currentFragment);
536543
this._currentFragment.setMenuVisibility(false);
537544
this._currentFragment.setUserVisibleHint(false);
538545
}
539546

547+
console.log('setPrimaryItem', position,fragment);
540548
if (fragment != null) {
541549
fragment.setMenuVisibility(true);
542550
fragment.setUserVisibleHint(true);

src/tabs/tabs.android.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, CoercibleProperty, Color, Enums, Font, Frame, ImageSource, Property, Utils, getTransformedText, isIOS } from '@nativescript/core';
1+
import { Application, CoercibleProperty, Color, Enums, Font, Frame, ImageSource, Property, Utils, getTransformedText, isIOS, View } from '@nativescript/core';
22
import { TabStrip } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-strip';
33
import { TabStripItem } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-strip-item';
44
import { TabContentItem } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-content-item';
@@ -14,13 +14,15 @@ const DEFAULT_ELEVATION = 4;
1414

1515
const TABID = '_tabId';
1616
const INDEX = '_index';
17+
const ownerSymbol = Symbol('_owner');
1718

1819
type PagerAdapter = new (owner: Tabs) => androidx.viewpager.widget.PagerAdapter;
1920

2021
// eslint-disable-next-line no-redeclare
2122
let PagerAdapter: PagerAdapter;
2223
let TabsBar: any;
2324
let appResources: android.content.res.Resources;
25+
let AttachStateChangeListener: any;
2426

2527
function makeFragmentName(viewId: number, id: number): string {
2628
return 'android:viewpager:' + viewId + ':' + id;
@@ -326,8 +328,33 @@ function initializeNativeClasses() {
326328
}
327329
}
328330

331+
@NativeClass
332+
@Interfaces([android.view.View.OnAttachStateChangeListener])
333+
class AttachListener extends java.lang.Object implements android.view.View.OnAttachStateChangeListener {
334+
constructor() {
335+
super();
336+
337+
return global.__native(this);
338+
}
339+
340+
onViewAttachedToWindow(view: android.view.View): void {
341+
const owner: View = view[ownerSymbol];
342+
if (owner) {
343+
owner._onAttachedToWindow();
344+
}
345+
}
346+
347+
onViewDetachedFromWindow(view: android.view.View): void {
348+
const owner: View = view[ownerSymbol];
349+
if (owner) {
350+
owner._onDetachedFromWindow();
351+
}
352+
}
353+
}
354+
329355
PagerAdapter = FragmentPagerAdapter;
330356
TabsBar = TabsBarImplementation;
357+
AttachStateChangeListener = new AttachListener();
331358
appResources = Application.android.context.getResources();
332359
}
333360

@@ -374,6 +401,7 @@ export class Tabs extends TabsBase {
374401
private _selectedItemColor: Color;
375402
private _unSelectedItemColor: Color;
376403
fragments: androidx.fragment.app.Fragment[] = [];
404+
private _attachedToWindow = false;
377405

378406
constructor() {
379407
super();
@@ -457,13 +485,36 @@ export class Tabs extends TabsBase {
457485
const nativeView: any = this.nativeViewProtected;
458486
this._tabsBar = nativeView.tabsBar;
459487

488+
// nativeView.addOnAttachStateChangeListener(AttachStateChangeListener);
489+
nativeView[ownerSymbol] = this;
490+
460491
const viewPager = nativeView.viewPager;
461492
viewPager.setId(this._androidViewId);
462493
this._viewPager = viewPager;
463494
this._pagerAdapter = viewPager.adapter;
464495
(this._pagerAdapter as any).owner = this;
465496
}
466497

498+
_onAttachedToWindow(): void {
499+
super._onAttachedToWindow();
500+
501+
// _onAttachedToWindow called from OS again after it was detach
502+
// TODO: Consider testing and removing it when update to androidx.fragment:1.2.0
503+
if (this._manager && this._manager.isDestroyed()) {
504+
return;
505+
}
506+
507+
this._attachedToWindow = true;
508+
this._viewPager.setCurrentItem(this.selectedIndex, false);
509+
}
510+
511+
_onDetachedFromWindow(): void {
512+
super._onDetachedFromWindow();
513+
this.disposeCurrentFragments();
514+
515+
this._attachedToWindow = false;
516+
}
517+
467518
public _loadUnloadTabItems(newIndex: number) {
468519
const items = this.items;
469520
if (!items) {
@@ -538,6 +589,9 @@ export class Tabs extends TabsBase {
538589
(this._pagerAdapter as any).owner = null;
539590
this._pagerAdapter = null;
540591

592+
this.nativeViewProtected.removeOnAttachStateChangeListener(AttachStateChangeListener);
593+
this.nativeViewProtected[ownerSymbol] = null;
594+
541595
this._tabsBar = null;
542596
this._viewPager = null;
543597
super.disposeNativeView();

0 commit comments

Comments
 (0)