Skip to content
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

Keep the tabs adapter over the life of the viewpager #3255

Merged
merged 2 commits into from
Feb 15, 2023
Merged

Keep the tabs adapter over the life of the viewpager #3255

merged 2 commits into from
Feb 15, 2023

Conversation

nikclayton
Copy link
Contributor

Make tabs var instead of val in MainPagerAdapter so it can be updated when tabs change.

Then detach the tabLayoutMediator, update the tabs, and call notifyItemRangeChanged in setupTabs().

This fixes a bug (not sure if it's this code, or in ViewPager2) where assigning a new adapter to the view pager seemed to result in a leak of one or more fragments. This wasn't user-visible, but it's a leak, and it becomes user-visible when fragments want to display menus.

This also fixes two other bugs:

  1. Be on the left-most tab. Scroll down a bit. Then modify the tabs at "Account preferences > tabs", but keep the left-most tab as-is.

    Then go back to MainActivity. Your reading position in the left-most tab has been jumped to the top.

  2. Be on any non-left-most tab. Then modify the tab list by reordering tabs (adding/removing tabs is also OK).

    Then go back to MainActivity. Your tab selection has been overridden, and the left-most tab has been selected.

Because the fragments are not destroyed unnecessarily your reading position is retained. And it remembers the tab you had selected, and as long as that tab is still present you will be returned to it, even if it's changed position in the list.

Fixes #3251

Make `tabs` `var` instead of `val` in `MainPagerAdapter` so it can be updated
when tabs change.

Then detach the `tabLayoutMediator`, update the tabs, and call
`notifyItemRangeChanged` in `setupTabs()`.

This fixes a bug (not sure if it's this code, or in ViewPager2) where
assigning a new adapter to the view pager seemed to result in a leak of one
or more fragments. This wasn't user-visible, but it's a leak, and it becomes
user-visible when fragments want to display menus.

This also fixes two other bugs:

1. Be on the left-most tab. Scroll down a bit. Then modify the tabs at
   "Account preferences > tabs", but keep the left-most tab as-is.

   Then go back to MainActivity. Your reading position in the left-most
   tab has been jumped to the top.

2. Be on any non-left-most tab. Then modify the tab list by reordering tabs
   (adding/removing tabs is also OK).

   Then go back to MainActivity. Your tab selection has been overridden,
   and the left-most tab has been selected.

Because the fragments are not destroyed unnecessarily your reading position
is retained. And it remembers the tab you had selected, and as long as that
tab is still present you will be returned to it, even if it's changed
position in the list.

Fixes #3251
# Conflicts:
#	app/src/main/java/com/keylesspalace/tusky/MainActivity.kt
@connyduck connyduck merged commit 196ebdb into tuskyapp:develop Feb 15, 2023
@nikclayton nikclayton deleted the 3251-leaking-fragments branch February 16, 2023 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Leaking fragments?
2 participants