heihei blog

Blog icon by Unsplash https://unsplash.com/@virussinside

Notes - Fragments: Past, Present, and Future (Android Dev Summit '19)



本記事は、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


  • 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 tag
      • does 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
    • ?


  • 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.


  • FragmentFactory、FragmentScenarioを使うことでFragmentの生成やテストがしやすくなったよ
    • Factoryを使うことでconstructor injectionも可能になったよ
  • FragmentをViewに内包する場合にはFragmentContainerViewを使う(FrameLayoutは使わない)
    • 内部ではFragmentManagerが利用されている
  • Googleとしては、将来的にももっともっと良くしていきたいFragment APIが沢山ある
    • 複数のバックスタックを可能にする(BottomNavの切替時とかの管理を簡単にしたい)
    • Fragment-Fragment間の値の受け渡しとかをより簡単にできたら(FragmnetActivityあたりへのAPI変更が数ヶ月以内にあるかも?)
    • Fragment自体のLifecycleとViewLifecycleを1つにできるとしたら...???