Question about focus and activites on Android
I have a chat application here that uses a handler and a runnable to frequently poll for updates. On receiving an update as HTML, it adds this content to the TextView in the main activity.
Now, I've noticed that when the activity loses focus and I use other applications/view the home screen, the runnable keeps going on and when I re-open the application, I see the TextView with the updated content.
Is this con开发者_运维问答sidered unsafe and should I build a service to do this, or is it fine to modify the UI when it's unfocused like that?
I don't believe that it's good for your applicaation to update the ui when it's not focused as the resources on some devices might be limited and the battery life will also be reduced because of the permanent cpu and data connection usage.
To implement this as a server seems to be overkill. Why don't you clear the handler queue with removeMessage when the onPause of the activity is called and fill it again during the onResume?
精彩评论