开发者

Activity or service in background task?

开发者 https://www.devze.com 2023-01-31 00:20 出处:网络
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

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开发者_StackOverflow. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消