7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LiveDataの更新が反映されない

Posted at

たまにLiveDataの更新がビューに反映されなくてハマります。
よくやるのが lifecycleOwnerを設定し忘れること。 これをやってしまうと初期値は反映されるが更新が反映されなくて、ViewModelの記述を見ても悪いところがなくて解決に時間がかかります。

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    viewModel = ViewModelProviders.of(this).get(HogeViewModel::class.java)
    binding = HogeFragmentBinding.inflate(inflater, container, false).apply {
        viewmodel = viewModel
        lifecycleOwner = viewLifecycleOwner // これを書き忘れる
    }
    return binding.root
}
7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?