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

Show fragments correctly in bottom navigation #11838

Merged
merged 4 commits into from
May 7, 2020

Conversation

planarvoid
Copy link
Contributor

Fixes #11830

Previously we were using FragmentManager.replace to replace fragments on bottom navigation change. This call sets the Fragment as not active. However, we were still keeping and using the fragments in the sparse array. Overall this approach works but it causes some unnecessary fragment recreation and it's buggy with child fragments. If the parent is not active, the child fragments are also not active (even if the parent is actually visible). I'm replacing this functionality with show/hide calls instead. The fragments get initialized the first time you open the app and when you switch the tabs (or rotate the device), the fragments are retrieved from the fragment manager and shown/hidden.

To test:

  • Smoke test the bottom navigation
  • Rotate the device
  • The scroll position is kept
  • Debug the code to see that the fragments are not unnecessarily created

PR submission checklist:

  • I have considered adding unit tests where possible.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

@planarvoid planarvoid added this to the 14.9 milestone May 7, 2020
@planarvoid planarvoid requested a review from malinajirka May 7, 2020 08:08
@planarvoid planarvoid self-assigned this May 7, 2020
@peril-wordpress-mobile
Copy link

peril-wordpress-mobile bot commented May 7, 2020

You can trigger optional UI/connected tests for these changes by visiting CircleCI here.

@planarvoid planarvoid changed the title Fix/add fragments correctly in bottom nav Show fragments correctly in bottom navigation May 7, 2020
Copy link
Contributor

@malinajirka malinajirka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @planarvoid! I'm curious what you think about using the fragmentmanager instead of caching the fragments locally - see my comment. I haven't tested the app yet.

@@ -281,31 +288,45 @@ class WPMainNavigationView @JvmOverloads constructor(
}

private inner class NavAdapter {
private val mFragments = SparseArray<Fragment>(numPages())
private val mFragments = mutableMapOf<PageType, Fragment>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to manually keep references to the fragments? I'm not sure it's safe - what if we unintentionally re-use a destroyed fragment. It won't go though it's regular restoration process and we might end up in an undefined state. I'd personally rather rely on the FragmentManager implementation for the fragment caching, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I've removed the map and simplified the adapter. I think now it's much better.

@peril-wordpress-mobile
Copy link

peril-wordpress-mobile bot commented May 7, 2020

You can test the changes on this Pull Request by downloading the APK here.

Copy link
Contributor

@malinajirka malinajirka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks and works as expected;). The issue with the ViewPager2 disappeared 🥳

@malinajirka malinajirka merged commit 94c87b4 into develop May 7, 2020
@malinajirka malinajirka deleted the fix/add-fragments-correctly-in-bottom-nav branch May 7, 2020 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stop fragments on bottom navigation change
2 participants