This article has been translated by Gemini.
In this Notes article, I roughly jotted down notes in English from the content of the English session video/podcast. This article is intended to be read as a reference while watching the video. I hope this article helps you even a little when you watch the actual video. (Please feel free to contact me if there are any errors or typos! m(__)m)
This article is based on Fragments: Past, Present, and Future (Android Dev Summit ‘19).
Fragment Past#
Android API 11
Fragments were really designed from the beginning to the kind of Micro Activities.
First step to move things from fat Activities to smaller fragments
this also meant that Fragments inherited a lot of APIs that Activity had
- ActionBar Menus
this also meant that there became a lot custom hooks
Things activity got received needed to be received on Fragments as well
onActivityResult
Permissions
Multi Window
Picture in Picture
Present#
Restructuring the goal of Fragments
focused API surfaces
core, simple, predictable no-surprise apis
without breaking changes
- deprecating old apis with new better APIs
Providing testable APIs
Fragment testing artifact
Fragment Scenario
moveToState(), recreate()
launchFragmentInContainer
Working together with AndroidX team
Instantiating Fragments
Recreation after config change
FragmentTransaction add / replace
FragmentScenario
FragmentFactory
Support constructor injection
No requirement for no arg constructor
One container for Fragments
FragmentContainerView
Use it instead of FrameLayout
Replace the
<fragment>tagdoes use FragmentManager under the hood
- Fix issue of replacing fragment later
Fix the Z-ordering animation issue
System back
OnBackPressedDispatcher
- Arch Components
ViewModels
by viewModels()
by navGraphViewModels(R.id.main)
by activityViewModels()
Lifecycle
- ?
Future#
Subject to change
multiple back stack
single stack: existing legacy on Android
allow saving and restoring a whole stack of fragments without losing state
Bottom nav made easy
returning results
how do I talk between fragments?
fragment API right now is not great
create a better API for startActivityForResult() and for passing a result to another Fragment
- building and hopefully sharing in a few months
lifecycle
fragment has two lifecycles
fragment itself
fragment’s view has totally different lifecycle
- view may get destroyed, but fragment still exists
what if, these two lifecycles are the same?
- bringing these things together, we can solve a lot of complexity. => maybe this kind of opt-in APIs will be available in FragmentActivity level.
Impressions / Summary / Key Takeaways#
- FragmentFactory and FragmentScenario make it easier to create and test Fragments.
- Using Factory also enables constructor injection.
- Use FragmentContainerView (instead of FrameLayout) when embedding Fragments in a View.
- FragmentManager is used internally.
- Google has many more Fragment APIs they want to improve in the future.
- Enabling multiple back stacks (to make management during BottomNav switching easier).
- Making passing values between Fragment-Fragment easier (API changes around FragmentActivity might come in a few months?).
- What if the Fragment’s own lifecycle and ViewLifecycle could be unified…???
Fragment APIs seem likely to become even easier to use and more intuitive.
That’s all!