2021年2月10日水曜日

Fragment.viewLifecycleOwnerLiveData で LiveData<LifecycleOwner?> が取れます

AndroidX の Fragment に getViewLifecycleOwnerLiveData() というメソッドがあります。
  1. @NonNull  
  2. public LiveData<LifecycleOwner> getViewLifecycleOwnerLiveData() {  
  3.     return mViewLifecycleOwnerLiveData;  
  4. }  
これは LiveData<LifecycleOwner?> を返してくれます。
この LiveData には、onCreateView() が non-null な View を返した後に getViewLifecycleOwner() で取得できるのと同じ LifecycleOwner がセットされ、onDestroyView() が呼ばれた後に null がセットされます。

実際に試してみましょう。
  1. class MainFragment : Fragment() {  
  2.   
  3.     private val observer = Observer<LifecycleOwner?> {  
  4.         println("--- lifecycleOwner : $it")  
  5.     }  
  6.   
  7.     override fun onCreate(savedInstanceState: Bundle?) {  
  8.         super.onCreate(savedInstanceState)  
  9.         println("onCreate")  
  10.   
  11.         viewLifecycleOwnerLiveData.observeForever(observer)  
  12.     }  
  13.   
  14.     override fun onAttach(context: Context) {  
  15.         super.onAttach(context)  
  16.         println("onAttach")  
  17.     }  
  18.   
  19.     override fun onCreateView(  
  20.         inflater: LayoutInflater,  
  21.         container: ViewGroup?,  
  22.         savedInstanceState: Bundle?  
  23.     ): View {  
  24.         println("onCreateView")  
  25.         return TextView(inflater.context)  
  26.     }  
  27.   
  28.     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {  
  29.         super.onViewCreated(view, savedInstanceState)  
  30.         println("onViewCreated")  
  31.     }  
  32.   
  33.     override fun onStart() {  
  34.         super.onStart()  
  35.         println("onStart")  
  36.     }  
  37.   
  38.     override fun onResume() {  
  39.         super.onResume()  
  40.         println("onResume")  
  41.     }  
  42.   
  43.     override fun onPause() {  
  44.         super.onPause()  
  45.         println("onPause")  
  46.     }  
  47.   
  48.     override fun onStop() {  
  49.         super.onStop()  
  50.         println("onStop")  
  51.     }  
  52.   
  53.     override fun onActivityCreated(savedInstanceState: Bundle?) {  
  54.         super.onActivityCreated(savedInstanceState)  
  55.         println("onActivityCreated")  
  56.     }  
  57.   
  58.     override fun onDestroyView() {  
  59.         super.onDestroyView()  
  60.         println("onDestroyView")  
  61.     }  
  62.   
  63.     override fun onDetach() {  
  64.         super.onDetach()  
  65.         println("onDetach")  
  66.     }  
  67.   
  68.     override fun onDestroy() {  
  69.         super.onDestroy()  
  70.         println("onDestroy")  
  71.         viewLifecycleOwnerLiveData.removeObserver(observer)  
  72.     }  
  73. }  
  1. : onAttach  
  2. : onCreate  
  3. : onCreateView  
  4. : --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@6493c3f  
  5. : onViewCreated  
  6. : onActivityCreated  
  7. : onStart  
  8. : onResume  
  9. : ----- detach ----- [ここで Activity から detach ]  
  10. : onPause  
  11. : onStop  
  12. : onDestroyView  
  13. : --- lifecycleOwner : null  
  14. : ----- attach ----- [ここで Activity に attach ]  
  15. : onCreateView  
  16. : --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@50fd6c  
  17. : onViewCreated  
  18. : onActivityCreated  
  19. : onStart  
  20. : onResume  
  21.  [ここで 画面回転 ]  
  22. : onPause  
  23. : onStop  
  24. : onDestroyView  
  25. : --- lifecycleOwner : null  
  26. : onDestroy  
  27. : onDetach  
  28. : onAttach  
  29. : onCreate  
  30. : onCreateView  
  31. : --- lifecycleOwner : androidx.fragment.app.FragmentViewLifecycleOwner@9f4c99a  
  32. : onViewCreated  
  33. : onActivityCreated  
  34. : onStart  
  35. : onResume  
  36.  [ここでバックキーを押して Activity を終了 ]  
  37. : onPause  
  38. : onStop  
  39. : onDestroyView  
  40. : --- lifecycleOwner : null  
  41. : onDestroy  
  42. : onDetach  
onCreateView() で TextView を返しているので onCreateView() の後に FragmentViewLifecycleOwner のインスタンスが流れてきて、onDestroyView() が呼ばれた後に null が流れてきています。


onCreateView() で null を返すと
  1. class MainFragment : Fragment() {  
  2.   
  3.     ...  
  4.   
  5.     override fun onCreateView(  
  6.         inflater: LayoutInflater,  
  7.         container: ViewGroup?,  
  8.         savedInstanceState: Bundle?  
  9.     ): View? {  
  10.         println("onCreateView")  
  11.         return null  
  12.     }  
  13.   
  14.     ...  
  15. }    
  1. : onAttach  
  2. : onCreate  
  3. : onCreateView  
  4. : onActivityCreated  
  5. : onStart  
  6. : onResume  
  7. : ----- detach -----  
  8. : onPause  
  9. : onStop  
  10. : onDestroyView  
  11. : --- lifecycleOwner : null  
  12. : ----- attach -----  
  13. : onCreateView  
  14. : onActivityCreated  
  15. : onStart  
  16. : onResume  
  17. : onPause  
  18. : onStop  
  19. : onDestroyView  
  20. : --- lifecycleOwner : null  
  21. : onDestroy  
  22. : onDetach  
  23. : onAttach  
  24. : onCreate  
  25. : onCreateView  
  26. : onActivityCreated  
  27. : onStart  
  28. : onResume  
  29. : onPause  
  30. : onStop  
  31. : onDestroyView  
  32. : --- lifecycleOwner : null  
  33. : onDestroy  
  34. : onDetach  
onCreateView() の後にはなにも流れてきませんが、onDestroyView() の後には null が流れてきます。



0 件のコメント:

コメントを投稿