简体   繁体   中英

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 considered 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?

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