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

前端 未结 4 1464
北恋
北恋 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:38

    It is just as simple as that, if you want to listen for events even when your activity is not visible then call unregister in onStop() (E.g From Activity A you open Activity B but if you want A to still listening for the events).

    But when you only want to listen only for events when your activity is visible then in onPause call unregister() (E.g From Activity A you opened Activity B but now you do not want to listen for events in activity A).

    Hope this helps your problem.

提交回复
热议问题