简体   繁体   中英

Activity or service in background task?

Here's my set-up; I have a news app. This app loads a few listviews from local storage (I'm abusing sharedpreferences for that, but if that's good practice would be another question). I want to be able to update this local storage smoothly.

My first approach was to make a button trigger a service, which did the trick. But I also wanted the service to refresh the activity when it was done. I wasn't able to do that. My current approach seems to work, but not as smoothly on 3G. Activity A shows a 'Loading' box, then launches activity B and destroys itself. Activity B also shows a loading box, does the downloading, then destroys itself and relaunches activity A with the new content. However, this last approach makes the screen go black for a few seconds in transition from A to B when on 3G.

For the sake of visual smoothness, I'd like one loading popup to display continuously. When done, the activity should be refreshed.

Does anyone know how I can achieve this?

I hope my explanation was clear enough. Otherwise, I'd be happy to share some code with you.

Thanks a lot in advance

Looking at the code you linked to in your comment, you are doing too much work in the onCreate method of your activity. onCreate should be kept lightweight in order to display UI to the user as quick as possible. You probably want to move the bulk of your Activity setup to the onResume method. Furthermore, anything that blocks the UI thread (like fetching and parsing data) should be done on a thread. Look at using AsyncTask ( http://developer.android.com/reference/android/os/AsyncTask.html ) for this purpose in your application.

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