Skip to content

fix(android): active tab not displaying after nav back to page #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public void setItems(TabItemSpec[] items) {

}

public void scrollToTab(int tabIndex) {
scrollToTab(tabIndex, 0);
}

/**
* Updates the UI of an item at specified index
*/
Expand Down
13 changes: 13 additions & 0 deletions src/tabs/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ export class Tabs extends TabNavigation<TabsBar> {

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

protected override selectTabBar(oldIndex: number, newIndex: number) {
Expand Down
1 change: 1 addition & 0 deletions src/typings/extensions.android.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare namespace com {

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

setSelectedPosition(position: number): void;
Expand Down