この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
fragmentメモ [2015/07/06 13:03] sou |
fragmentメモ [2015/07/29 09:51] (現在) sou |
||
---|---|---|---|
ライン 21: | ライン 21: | ||
\\ | \\ | ||
=== Fragment切り替え時のIllegalStateException === | === Fragment切り替え時のIllegalStateException === | ||
+ | |||
+ | 下記の「操作NG」の箇所でFragmentを操作しようとすると発生します。\\ | ||
+ | |||
+ | [Fragmentライフサイクル]\\ | ||
+ | ...\\ | ||
+ | ↓\\ | ||
+ | ↓ 操作NG\\ | ||
+ | ↓\\ | ||
+ | onViewCreated(Bundle savedInstanceState) 状態復元\\ | ||
+ | onResume()\\ | ||
+ | ↓\\ | ||
+ | ↓\\ | ||
+ | ↓ 操作OK\\ | ||
+ | ↓\\ | ||
+ | ↓\\ | ||
+ | onPause()\\ | ||
+ | onSaveInstanceState() 状態保存\\ | ||
+ | ↓\\ | ||
+ | ↓ 操作NG\\ | ||
+ | ↓\\ | ||
+ | ...\\ | ||
+ | |||
+ | 「操作OK」の箇所で操作すれば大丈夫なように見えますが、非同期(サブスレッド)でFragmentTransacitonを取り扱ってしまったり、 | ||
+ | そもそもFragmentTransaction#commit()が非同期であるために発生します。 | ||
+ | |||
+ | |||
+ | FragmentのIllegalStateException回避\\ | ||
http://iti.hatenablog.jp/entry/2015/02/23/121110\\ | http://iti.hatenablog.jp/entry/2015/02/23/121110\\ | ||
Fragment使用時のIllegalStateException回避\\ | Fragment使用時のIllegalStateException回避\\ | ||
ライン 29: | ライン 56: | ||
http://stackoverflow.com/questions/8040280/how-to-handle-handler-messages-when-activity-fragment-is-paused | http://stackoverflow.com/questions/8040280/how-to-handle-handler-messages-when-activity-fragment-is-paused | ||
\\ | \\ | ||
+ | |||
+ | === FragmentTransaction#commit()はメインスレッドのHandlerにメッセージキューが積まれる === | ||
+ | FragmentManager#beginTransaction→(FragmentManagerImpl#beginTransaction)\\ | ||
+ | (Fragmentをいろいろ操作)\\ | ||
+ | FragmentTransaction#commit→(android.app.BackStackRecord#commit)\\ | ||
+ | (FragmentManagerImpl#enqueueAction)→(Activity#mHandlerに処理委譲)\\ | ||
+ | |||
+ | |||
=== Fragment in Fragment === | === Fragment in Fragment === | ||
FragmentでFragmentを管理するときはgetChildFragmentManager()\\ | FragmentでFragmentを管理するときはgetChildFragmentManager()\\ |