Retained Fragments with UI and memory leaks

后端 未结 5 1385
刺人心
刺人心 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:56

    Be careful when retaining certain objects that are coupled to the Activity.

    Caution: While you can return any object, you should never pass an object that is tied to the Activity, such as a Drawable, an Adapter, a View or any other object that's associated with a Context. If you do, it will leak all the views and resources of the original activity instance. (Leaking resources means that your application maintains a hold on them and they cannot be garbage-collected, so lots of memory can be lost.)

    http://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject

提交回复
热议问题