Retained Fragments with UI and memory leaks

后端 未结 5 1376
刺人心
刺人心 2020-12-02 05:06

I\'ve read that setting .setOnRetainInstance(true) on fragments presenting UI may lead to memory leaks.

Could somebody please explain why and how this

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 05:41

    setRetainInstance(true) is used to retain instances of dynamic Fragments during an Activity recreation, such as a screen rotation or other config changes. This does not mean the Fragment will be retained forever by the system though.

    When an Activity is terminated for other reasons, such as the user finishing the Activity (i.e. pressing back), the Fragment should be eligible for garbage collection.

提交回复
热议问题