Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
最新のFragment事情 / Fragment new features
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
star_zero
November 21, 2019
Programming
3.3k
8
Share
最新のFragment事情 / Fragment new features
star_zero
November 21, 2019
More Decks by star_zero
See All by star_zero
今からはじめるAndroidアプリ開発 2024 / DevFest 2024
star_zero
0
1.6k
Jetpack Compose の Side-effect を使いこなす / DroidKaigi 2023
star_zero
5
7k
Android 14 新機能 / Android 14 Meetup Nagoya
star_zero
1
660
Android 14 と Predictive back gesture / Shibuya.apk #42
star_zero
0
480
Coroutines Test 入門 / Android Test Night #8
star_zero
2
1.3k
What's new in Jetpack / I/O Extended Japan 2022
star_zero
1
700
Kotlin 2021 Recap / DevFest 2021
star_zero
3
1.4k
Kotlin Symbol Processing (KSP) を使ったコード生成 / DroidKaigi 2021
star_zero
2
5.3k
What's new Android 12
star_zero
0
620
Other Decks in Programming
See All in Programming
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
3
800
Terraform言語の静的解析 / static analysis of Terraform language
wata727
1
140
書き換えて学ぶTemporal #fukts
pirosikick
2
370
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
490
ふにゃっとしない名前の付け方 〜哲学で茹で上げる、コシのあるソフトウェア設計〜
shimomura
0
120
AWSはOSSをどのように 考えているのか?
akihisaikeda
0
110
アクセシビリティ試験の"その後"を仕組み化する
yuuumiravy
1
200
リセットCSSを1行消したらアクセシビリティが向上した話
pvcresin
4
510
Agentic UI in the Frontend: Architectures with Open Standards @JAX 2026 in Mainz
manfredsteyer
PRO
0
110
[RubyKaigi 2026] Require Hooks
palkan
1
310
Road to RubyKaigi: Play Hard(ware)
makicamel
1
570
🦞OpenClaw works with AWS
licux
1
350
Featured
See All Featured
[SF Ruby Conf 2025] Rails X
palkan
2
1k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
120
RailsConf 2023
tenderlove
30
1.4k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
44k
The SEO Collaboration Effect
kristinabergwall1
1
440
Agile that works and the tools we love
rasmusluckow
331
21k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.1k
Producing Creativity
orderedlist
PRO
348
40k
Code Review Best Practice
trishagee
74
20k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
170
Transcript
最新のFragment事情 Android感謝祭 2019/11/21
About me •Kenji Abe •クックパッド株式会社 •Google Developers Expert for Android
•Twitter: @STAR_ZERO
Update Fragment
⚠今回の環境⚠ •Fragment 1.2.0 rc-02 ‣ 2019/11/21時点の最新
Constructor with LayoutId
Constructor with LayoutId •コンストラクタにLayoutIdを渡せるようになった •DataBinding/ViewBindingは非対応 class SampleFragment : Fragment(R.layout.fragment_sample)
Constructor with LayoutId https://developer.android.com/reference/androidx/fragment/app/Fragment.html
FragmentContainerView
FragmentContainerView •これからはFrameLayoutではなくコチラが推奨 •アニメーションのz-orderなどのバグがあった •https://issuetracker.google.com/issues/37036000
FragmentContainerView <androidx.fragment.app.FragmentContainerView android:id="@+id/container" android:name="com.example.SampleFragment" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"
/>
OnBackPressedDispatcher
OnBackPressedDispatcher •Fragmentでバックキーの制御が可能になった •Lifecycleによってコールバックの追加、削除が簡単 •コールバックの有効、無効の切り替えも可能
OnBackPressedDispatcher class SampleFragment : Fragment() { private val callback =
object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { // バックキーが押されたとき... } } override fun onAttach(context: Context) { super.onAttach(context) requireActivity().onBackPressedDispatcher .addCallback(this, callback) } }
OnBackPressedDispatcher class SampleFragment : Fragment() { // ... fun someFunction()
{ callback.isEnabled = false requireActivity().onBackPressedDispatcher.onBackPressed() } }
OnBackPressedDispatcher class MainActivity : AppCompatActivity() { override fun onBackPressed() {
// FragmentのhandleOnBackPressedの後に呼び出される if (onBackPressedDispatcher.hasEnabledCallbacks()) { // Fragmentのバックキー制御が有効 } else { // Fragmentのバックキー制御が無効 } } }
FragmentFactory
FragmentFactory •Fragmentの生成を行うクラス •Fragmentのコンストラクタに引数が渡せるようになる ‣ コンストラクタインジェクションが可能 •引数なしのコンストラクタ不要 •FragmentScenarioでMock
FragmentFactory class SampleFactory: FragmentFactory() { override fun instantiate( classLoader: ClassLoader,
className: String ): Fragment { return when (className) { SampleFragment::class.java.name -> SampleFragment("ABC") else -> super.instantiate(classLoader, className) } } }
FragmentFactory class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)
{ supportFragmentManager.fragmentFactory = SampleFactory() super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // fragment-ktx supportFragmentManager.commit { add<SampleFragment>(R.id.container) } } }
FragmentFactory @RunWith(AndroidJUnit4::class) class SampleFragmentTest { @Test fun textSample() { val
mockFactory = MockFactory() val scenario = launchFragmentInContainer<SampleFragment>( factory = mockFactory ) onView(withId(R.id.text)).check(matches(withText("Hello World!"))) } }
FragmentFactory •Daggerで使うには? ‣ https://satoshun.github.io/2018/11/android_all_inject_ctor/
RESUME_ONLY_CURRENT_FRAGMENT
RESUME_ONLY_CURRENT_FRAGMENT •ViewPagerでonResumeが呼ばれるタイミングが変わる ‣ これまでは表示されてなくてもonResumeが呼ばれてた ‣ 表示されてるFragmentのonResumeのみが呼ばれる •setUserVisibleHintはDeprecated
RESUME_ONLY_CURRENT_FRAGMENT class SampleAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { // 以前の挙動にしたい場合は
BEHAVIOR_SET_USER_VISIBLE_HINT override fun getItem(position: Int): Fragment { // ... } override fun getCount(): Int { // ... } }
fragment-ktx
fragment-ktx supportFragmentManager.commit { add<SampleFragment>(R.id.container) } val viewModel: SampleViewModel by viewModels()
val viewModel: SampleViewModel by activityViewModels() val viewModel: SampleViewModel by viewModels { viewModelFactory }
lint
lint
lint
今後のFragment
今後のFragment https://www.youtube.com/watch?v=RS1IACnZLy4
今後のFragment •Multiple Back Stacks •Fragmentの戻り値 •Lifecycleの改善
ありがとうございました