When to unregister BroadcastReceiver? In onPause(), onDestroy(), or onStop()?

前端 未结 4 1450
北恋
北恋 2020-12-14 15:23

When should I use unregisterReceiver? In onPause(), onDestroy(), or onStop()?

Note: I need the service to run in the backgroun

4条回答
  •  别那么骄傲
    2020-12-14 15:48

    From the Android documentation:

    You should implement onStop() to release activity resources such as a network connection or to unregister broadcast receivers.

    Then, I would follow these pairs (using @StinePike's analogy):

    onResume - onPause
    onStart  - onStop
    

    Because of the Android Lifecycle, and as @w3bshark mentioned:

    In post-HoneyComb (3.0+) devices, onStop() is the last guaranteed handler.

提交回复
热议问题