Keeping a reference to a View in a Fragment causes memory leaks?

后端 未结 6 1090
难免孤独
难免孤独 2021-02-04 13:30

Somebody told me the following, but I am a bit perplexed.

Please, would you be able to confirm or dispute it?

(the Fragment is

6条回答
  •  情书的邮戳
    2021-02-04 13:52

    There's a Fragment lifecycle method called onDestroyView which you should override to release any reference to the views.

    Generally you should only use lateinit var view references if your Fragment is permanently added to the Activity and it will not be removed.

    Kotlin View Binding extensions already solve this problem by automatically clearing view cache inside onDestroyView.

提交回复
热议问题