“Do not place Android context classes in static fields; this is a memory leak” - Lint Warning for static View

后端 未结 2 1172
猫巷女王i
猫巷女王i 2021-01-17 05:12

There are questions with a similar title, but them all about Context that you get in constructor.

There are RecyclerView with items and some other v

2条回答
  •  庸人自扰
    2021-01-17 05:45

    Do not put widgets in static fields.

    Options include:

    1. Delete this class. Move all of this logic into the activity (or fragment), where you have direct access to the widgets.

    2. Use an event bus (LocalBroadcastManager, greenrobot's EventBus, etc.). Have your code here post messages on the bus when the state changes. Have your UI (activity or fragment) subscribe for messages on the bus and update the widgets.

    3. Have your activity/fragment hold an instance of CommentsAudioPlayer, and make the fields in CommentsAudioPlayer non-static.

    Of the three, the first option would be simpler, cleaner, less memory-intensive, and faster to execute.

提交回复
热议问题