heihei blog

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

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

f:id:shaunkawano:20191025202341p:plain

※Notes記事では、英語のセッション動画やポッドキャストの内容を(雑に)英語でメモに書き残すことを行っています。本記事は、あくまで動画を見ながら、参考程度に読んでいただくことを想定しています。Notes記事には雑メモ程度のものだったり、書き起こしのようなものもあります。これから実際の動画を見る際には、本記事の内容が少しでもお役に立てば幸いです。(内容において不備、誤字脱字等ありましたら気軽にご連絡いただけると嬉しいです!m(__)m)

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

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.

所感・まとめ・個人的に印象に残ったことなど

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

FramgentのAPIはより使いやすく、直感的なものになっていきそうですね。

以上です!