Slide 1

Slide 1 text

Deep Dive into RememberSaveable

Slide 2

Slide 2 text

yurihondo 2 HELLO! Software Engineer@U-NEXT とにかく家が欲しい。 いい物件があったら教えてください。 Twitter : yurihondo @yuyuyuyuyuri

Slide 3

Slide 3 text

Agenda 3 » Background » Understanding the Inner Workings » How to Customize

Slide 4

Slide 4 text

BACKGROUND 4 1.

Slide 5

Slide 5 text

Background Composeを使っていると アプリ パフォーマンスについて課題が発生すること がたま〜にある 5

Slide 6

Slide 6 text

Background 弊チーム 場合、 Nexusなど 少し前()に発売された端末などで アプリ パフォーマンス劣化が顕著だった 6

Slide 7

Slide 7 text

Background インスタンス化 処理が原因 一つ 7

Slide 8

Slide 8 text

Background 8 https://speakerdeck.com/tomoya0x00/compose-false-lazycolumn-pahuomansugai-shan-dequ-rizu-ndakoto

Slide 9

Slide 9 text

Background 保存するオブジェクトが多いシチュエーションに おいて 、 RememberXxxなどを利用する際、 どこにキャッシュされる かを 気にした方が良い (大体 アプリ 気にしなくて良い) 9

Slide 10

Slide 10 text

Background rememberSaveable 保存先(SavedState)に 、 保存量に制限がある 10

Slide 11

Slide 11 text

Background Bundleな でMax 1MB…と思いきや、 11 https://developer.android.com/guide/components/activities/parcelables-and-bundles

Slide 12

Slide 12 text

Background SavedStateについて 、 50KB以上保存する 避けた方が良さそう 12

Slide 13

Slide 13 text

Background なんか前 大丈夫だった気がする... と思ったら、M以前 Warningだったらしい 13

Slide 14

Slide 14 text

Background 14 今 使い勝手を維持しつつ なんとかならん か?

Slide 15

Slide 15 text

Understanding the Inner Workings 15 2.

Slide 16

Slide 16 text

Background 16 ここから 発表内容 Androidx.compose.runtime:runtime-saveable: 1.4.0 環境ベースです

Slide 17

Slide 17 text

RememberSaveable Understanding the Inner Workings 17

Slide 18

Slide 18 text

RememberSaveable Understanding the Inner Workings 18 データ 保存や復元時に ペアになるKeyを生成

Slide 19

Slide 19 text

RememberSaveable Understanding the Inner Workings 19 SaveableStateRegistryからKeyを元に 保存していたデータを取り出す

Slide 20

Slide 20 text

RememberSaveable Understanding the Inner Workings 20 データや保存/復元を担う処理 変更 を監視

Slide 21

Slide 21 text

RememberSaveable Understanding the Inner Workings 21

Slide 22

Slide 22 text

RememberSaveable Understanding the Inner Workings 22 保存処理 Lambdaを SaveableStateRegistryへ登録

Slide 23

Slide 23 text

RememberSaveable Understanding the Inner Workings 23 Compositionを抜ける時に、 登録した保存処理を消す

Slide 24

Slide 24 text

RememberSaveable Understanding the Inner Workings 24 Compositionを抜ける時に、 登録した保存処理を消す

Slide 25

Slide 25 text

Understanding the Inner Workings 25

Slide 26

Slide 26 text

Understanding the Inner Workings 26 これを知りたい

Slide 27

Slide 27 text

Understanding the Inner Workings 27 SaveableStateRegistry

Slide 28

Slide 28 text

SaveableStateRegistry Understanding the Inner Workings 28 » SavedInstanceState 仕組みを利用 してデータ 保存・復元をする » 保存するデータを生成するLambdaを 管理(登録、登録解除)する » データがこ 仕組みで保存可能なも かを判定する

Slide 29

Slide 29 text

SaveableStateRegistry Understanding the Inner Workings 29 » SavedInstanceState 仕組みを利用 してデータ 保存・復元をする » 保存するデータを生成するLambdaを 管理(登録、登録解除)する » データがこ 仕組みで保存可能なも かを判定する データ 保存 これをトリガーしているところを知りたい

Slide 30

Slide 30 text

Understanding the Inner Workings 30 SaveableStateRegistryを 実装している

Slide 31

Slide 31 text

Understanding the Inner Workings 31

Slide 32

Slide 32 text

Understanding the Inner Workings 32 注目すべき DisposableSaveableStateRegistory

Slide 33

Slide 33 text

Understanding the Inner Workings 33 ComposeViewがActivityなどに Attachされた時に呼 れ、 Composition Localに設定される

Slide 34

Slide 34 text

Disposable SaveableStateRegistory Understanding the Inner Workings 34 SavedStateRegistoryOwnerから SavedStateRegistoryを取得 keyを元に保存したデータを取得

Slide 35

Slide 35 text

Understanding the Inner Workings 35 SavedStateRegistoryに SaveableStateRegitoryが管理している 保存対象データ 取得処理を渡す Disposable SaveableStateRegistory

Slide 36

Slide 36 text

Understanding the Inner Workings 36 Activity#onSaveInstanceState タイミングで実行される

Slide 37

Slide 37 text

Understanding the Inner Workings 37 disposeが呼 れたタイミングで解除 Disposable SaveableStateRegistory

Slide 38

Slide 38 text

Understanding the Inner Workings 38 まとめると

Slide 39

Slide 39 text

Understanding the Inner Workings 39

Slide 40

Slide 40 text

Understanding the Inner Workings 40

Slide 41

Slide 41 text

Activity Composable Understanding the Inner Workings 41 RememberSaveable SaveableStateRegitory (DisposableSaveableStateRegistory) SavedStateRegitory

Slide 42

Slide 42 text

Understanding the Inner Workings 42 仕組み わかった これを応用すると...

Slide 43

Slide 43 text

How to Customize 43 3.

Slide 44

Slide 44 text

Activity Composable Understanding the Inner Workings 44 RememberSaveable SaveableStateRegitory (DisposableSaveableStateRegistory) SavedStateRegitory

Slide 45

Slide 45 text

Activity Composable Understanding the Inner Workings 45 RememberSaveable SaveableStateRegitory (DisposableSaveableStateRegistory) SavedStateRegitory

Slide 46

Slide 46 text

Activity Composable Understanding the Inner Workings 46 RememberSaveable SaveableStateRegitory (DisposableSaveableStateRegistory) SaveStateRegitory 保存方法を変えれ 保存先を変更できる

Slide 47

Slide 47 text

Understanding the Inner Workings 47 試しに作ってみよう

Slide 48

Slide 48 text

Activity Composable Understanding the Inner Workings 48 RememberSaveable SaveableStateRegitory (DisposableSaveableStateRegistory) SavedStateRegitory

Slide 49

Slide 49 text

How to Customize 49

Slide 50

Slide 50 text

How to Customize 50 SavedStateRegistoryに相当するI/F

Slide 51

Slide 51 text

How to Customize 51 単純に データ 保存領域として stateMap State 提供方法としてproviders を保持

Slide 52

Slide 52 text

How to Customize 52

Slide 53

Slide 53 text

How to Customize 53

Slide 54

Slide 54 text

Activity Composable Understanding the Inner Workings 54 RememberSaveable SaveableStateRegitory (DisposableSaveableStateRegistory) SaveStateRegitory

Slide 55

Slide 55 text

How to Customize 55

Slide 56

Slide 56 text

How to Customize 56 自前で用意したSavedStateRegistoryを取得

Slide 57

Slide 57 text

How to Customize 57 メイン 処理 Framework 実装に任せる

Slide 58

Slide 58 text

How to Customize 58

Slide 59

Slide 59 text

How to Customize 59 Composition Localに設定

Slide 60

Slide 60 text

How to Customize 60

Slide 61

Slide 61 text

How to Customize 61 自作 SavedStateRegistory Onwerを 実装、保存先を提供

Slide 62

Slide 62 text

How to Customize 62 OnSaveInstanceStateで保存をトリガーしておく

Slide 63

Slide 63 text

How to Customize 63 自作 SaveableStateRegistoryを提供

Slide 64

Slide 64 text

Understanding the Inner Workings 64 …特に必要性がない場合 、 いつも通りSavedStateに 保存したいよなぁ

Slide 65

Slide 65 text

Understanding the Inner Workings 65 rememberSaveableと 同じような仕組み自体を つくった方が良い で

Slide 66

Slide 66 text

Activity Composable Understanding the Inner Workings 66 RememberSaveable SaveableStateRegitory (DisposableSaveableStateRegistory) SaveStateRegitory つまり 第3 Remember を作れ ...

Slide 67

Slide 67 text

Understanding the Inner Workings 67 GitHubにサンプル上げている で 必要に応じてご活用ください。 https://github.com/yurihondo/OutlawRemember/tree/main/sample/src/main/java/com/ yurihondo/outlawremember/sample

Slide 68

Slide 68 text

Any questions? You can find me at: » @yuyuyuyuyuri on Twitter 68 THANKS!