Skip to content

Commit 9f985f2

Browse files
authored
Merge pull request #436 from dgmachado/bugfix/active-tab-not-displaying
fix(android): active tab not displaying after nav back to page
2 parents 4967955 + 4d8afcf commit 9f985f2

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

packages/core-tabs/platforms/android/java/com/nativescript/material/core/TabsBar.java

+4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ public void setItems(TabItemSpec[] items) {
184184

185185
}
186186

187+
public void scrollToTab(int tabIndex) {
188+
scrollToTab(tabIndex, 0);
189+
}
190+
187191
/**
188192
* Updates the UI of an item at specified index
189193
*/

src/tabs/index.android.ts

+13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ export class Tabs extends TabNavigation<TabsBar> {
8686

8787
protected override setTabBarItems(tabItems: com.nativescript.material.core.TabItemSpec[]) {
8888
this.mTabsBar.setItems(tabItems);
89+
// The setTimeout below is necessary to ensure the scrollToTab is executed only after
90+
// all tabs are recreated. The tabs' recreation is triggered by the setItems call above.
91+
//
92+
// The setTimeout is necessary to fix an Android issue:
93+
// Android Issue: Active Tab item not displaying after nav back
94+
// Reproduce steps:
95+
// 1. On app with multiple (overflown) tab items, Switch to the last tab item
96+
// 2. Navigate to a new page
97+
// 3. Nav back to the page with Tabs
98+
// 4. Notice the active last tab item is not showing. The tab strip is showing the most left / initial tab items instead.
99+
setTimeout(() => {
100+
this.mTabsBar.scrollToTab(this.selectedIndex);
101+
}, 0);
89102
}
90103

91104
protected override selectTabBar(oldIndex: number, newIndex: number) {

src/typings/extensions.android.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ declare namespace com {
4040

4141
setViewPager(viewPager: androidx.viewpager2.widget.ViewPager2): void;
4242
setItems(items: Array<TabItemSpec>): void;
43+
scrollToTab(tabIndex: Int);
4344
updateItemAt(position: number, itemSpec: TabItemSpec): void;
4445

4546
setSelectedPosition(position: number): void;

0 commit comments

Comments
 (0)