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

后端 未结 2 1140
猫巷女王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:42

    You should decouple view (mPlayPauseButton) from that functionality to avoid leaks. To do this, you can implement a listener pattern. The easier way in that code could be passing a "listener" object as a parameter instead directly the view reference...

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题