AndroidX の Fragment に
getViewLifecycleOwnerLiveData() というメソッドがあります。
- @NonNull
- public LiveData<LifecycleOwner> getViewLifecycleOwnerLiveData() {
- return mViewLifecycleOwnerLiveData;
- }
@NonNull
public LiveData<LifecycleOwner> getViewLifecycleOwnerLiveData() {
return mViewLifecycleOwnerLiveData;
}
これは LiveData<LifecycleOwner?> を返してくれます。
この LiveData には、onCreateView() が non-null な View を返した後に getViewLifecycleOwner() で取得できるのと同じ LifecycleOwner がセットされ、onDestroyView() が呼ばれた後に null がセットされます。
実際に試してみましょう。
- class MainFragment : Fragment() {
-
- private val observer = Observer<LifecycleOwner?> {
- println("--- lifecycleOwner : $it")
- }
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- println("onCreate")
-
- viewLifecycleOwnerLiveData.observeForever(observer)
- }
-
- override fun onAttach(context: Context) {
- super.onAttach(context)
- println("onAttach")
- }
-
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View {
- println("onCreateView")
- return TextView(inflater.context)
- }
-
- override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
- super.onViewCreated(view, savedInstanceState)
- println("onViewCreated")
- }
-
- override fun onStart() {
- super.onStart()
- println("onStart")
- }
-
- override fun onResume() {
- super.onResume()
- println("onResume")
- }
-
- override fun onPause() {
- super.onPause()
- println("onPause")
- }
-
- override fun onStop() {
- super.onStop()
- println("onStop")
- }
-
- override fun onActivityCreated(savedInstanceState: Bundle?) {
- super.onActivityCreated(savedInstanceState)
- println("onActivityCreated")
- }
-
- override fun onDestroyView() {
- super.onDestroyView()
- println("onDestroyView")
- }
-
- override fun onDetach() {
- super.onDetach()
- println("onDetach")
- }
-
- override fun onDestroy() {
- super.onDestroy()
- println("onDestroy")
- viewLifecycleOwnerLiveData.removeObserver(observer)
- }
- }
class MainFragment : Fragment() {
private val observer = Observer<LifecycleOwner?> {
println("--- lifecycleOwner : $it")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
println("onCreate")
viewLifecycleOwnerLiveData.observeForever(observer)
}
override fun onAttach(context: Context) {
super.onAttach(context)
println("onAttach")
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
println("onCreateView")
return TextView(inflater.context)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
println("onViewCreated")
}
override fun onStart() {
super.onStart()
println("onStart")
}
override fun onResume() {
super.onResume()
println("onResume")
}
override fun onPause() {
super.onPause()
println("onPause")
}
override fun onStop() {
super.onStop()
println("onStop")
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
println("onActivityCreated")
}
override fun onDestroyView() {
super.onDestroyView()
println("onDestroyView")
}
override fun onDetach() {
super.onDetach()
println("onDetach")
}
override fun onDestroy() {
super.onDestroy()
println("onDestroy")
viewLifecycleOwnerLiveData.removeObserver(observer)
}
}
- : onAttach
- : onCreate
- : onCreateView
- : --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@6493c3f
- : onViewCreated
- : onActivityCreated
- : onStart
- : onResume
- : ----- detach ----- [ここで Activity から detach ]
- : onPause
- : onStop
- : onDestroyView
- : --- lifecycleOwner : null
- : ----- attach ----- [ここで Activity に attach ]
- : onCreateView
- : --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@50fd6c
- : onViewCreated
- : onActivityCreated
- : onStart
- : onResume
- [ここで 画面回転 ]
- : onPause
- : onStop
- : onDestroyView
- : --- lifecycleOwner : null
- : onDestroy
- : onDetach
- : onAttach
- : onCreate
- : onCreateView
- : --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@9f4c99a
- : onViewCreated
- : onActivityCreated
- : onStart
- : onResume
- [ここでバックキーを押して Activity を終了 ]
- : onPause
- : onStop
- : onDestroyView
- : --- lifecycleOwner : null
- : onDestroy
- : onDetach
: onAttach
: onCreate
: onCreateView
: --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@6493c3f
: onViewCreated
: onActivityCreated
: onStart
: onResume
: ----- detach ----- [ここで Activity から detach ]
: onPause
: onStop
: onDestroyView
: --- lifecycleOwner : null
: ----- attach ----- [ここで Activity に attach ]
: onCreateView
: --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@50fd6c
: onViewCreated
: onActivityCreated
: onStart
: onResume
[ここで 画面回転 ]
: onPause
: onStop
: onDestroyView
: --- lifecycleOwner : null
: onDestroy
: onDetach
: onAttach
: onCreate
: onCreateView
: --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@9f4c99a
: onViewCreated
: onActivityCreated
: onStart
: onResume
[ここでバックキーを押して Activity を終了 ]
: onPause
: onStop
: onDestroyView
: --- lifecycleOwner : null
: onDestroy
: onDetach
onCreateView() で TextView を返しているので onCreateView() の後に FragmentViewLifecycleOwner のインスタンスが流れてきて、onDestroyView() が呼ばれた後に null が流れてきています。
onCreateView() で null を返すと
- class MainFragment : Fragment() {
-
- ...
-
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- println("onCreateView")
- return null
- }
-
- ...
- }
class MainFragment : Fragment() {
...
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
println("onCreateView")
return null
}
...
}
- : onAttach
- : onCreate
- : onCreateView
- : onActivityCreated
- : onStart
- : onResume
- : ----- detach -----
- : onPause
- : onStop
- : onDestroyView
- : --- lifecycleOwner : null
- : ----- attach -----
- : onCreateView
- : onActivityCreated
- : onStart
- : onResume
- : onPause
- : onStop
- : onDestroyView
- : --- lifecycleOwner : null
- : onDestroy
- : onDetach
- : onAttach
- : onCreate
- : onCreateView
- : onActivityCreated
- : onStart
- : onResume
- : onPause
- : onStop
- : onDestroyView
- : --- lifecycleOwner : null
- : onDestroy
- : onDetach
: onAttach
: onCreate
: onCreateView
: onActivityCreated
: onStart
: onResume
: ----- detach -----
: onPause
: onStop
: onDestroyView
: --- lifecycleOwner : null
: ----- attach -----
: onCreateView
: onActivityCreated
: onStart
: onResume
: onPause
: onStop
: onDestroyView
: --- lifecycleOwner : null
: onDestroy
: onDetach
: onAttach
: onCreate
: onCreateView
: onActivityCreated
: onStart
: onResume
: onPause
: onStop
: onDestroyView
: --- lifecycleOwner : null
: onDestroy
: onDetach
onCreateView() の後にはなにも流れてきませんが、onDestroyView() の後には null が流れてきます。
0 件のコメント:
コメントを投稿