简体   繁体   中英

best practice where to register broadcast receiver?

quick opinion question on where its 'best' to register a receiver? In my case my service sends a broadcast each time the UI needs updating.

Options as i understand them. Manifest. Oncreate. Onstart. Onresume

With the corresponding unregister of course.

I think for a UI update scenario makes sense to put in onresume and onpause... your thoughts?

If the receiver is only to receive events that would cause you to update the GUI then there are two options. Register to receive them in OnResume and unregister in OnPause if you want it to only update when the activity is in the foreground. OR between onStart and onStop if you want to update when the activity is even partially visible (ie a modal screen is partially blocking the activity). See here about 2 pages down. Look for "There are three key loops you may be interested in monitoring within your activity:"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM