开发者

One loader for all activities

Im building an Android application. It parses a feed which is stored in a DB. Each activity of the app is able to get this data from the DB.

Every activity can also call the Service, and make it refresh the data. When this is done I would like to display a loader. While the Service is downloading, the user is still free to navigate between activities. My question is; how can I displa开发者_开发问答y a loader that runs in all activities the user navigates to?

Thanks a lot :)


Here's what I would try to do since it seems to be an uncommon task to me:

I would try to setup a translucent PopupWindow which contains a progress indicator. I would trigger / dismiss this popup to be displayed / or not whenever there's a need to indicate the loading progress...


You could try something like:

  • Place a "global loader view" in all your activities.
  • When you start/return to an activity, fire an AsyncTask which will be used to handle updates of the global loader
  • Override onPreExecute() to prepare the global loader (e.g. setting its state to the current level if the service has been downloading for a while)
  • Override onProgressUpdate() and use it to update the state of the global loader by asking the Service for the current state (load percentage or something)
  • In doInBackground you implement:

    while( service.isLoading() ) {
      publishProgress( service.getLoadPercentage() )
      // Maybe add a Thread.sleep() here.
    }
    
  • Override onPostExecute to set the visibility of the global loader to View.GONE.

You might need to implement a way of killing the AsyncTask if the user switches away from the activity when the loading is not finished yet.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜