2

I need a little information/help/suggestions how can I build my application so I can use one AsyncTask from all activities which will download some data over internet. The problem is that once I run the asynctask from one activity, I want to download whole information again but to update only the information which I'm showing in that activity (like listviews, showing the images and etc.). And I'm not really sure how can I code this.

If anyone can offer me some help/suggestions even example it would be great! Thanks a lot!

Android-Droid
  • 14,365
  • 41
  • 114
  • 185

1 Answers1

3

The most common way we use AsyncTask is defined it as a inner class of a Activity. By doing this, we bind the AsyncTask's life cycle to its master - Activity, As we know, the Activity's life cycle is quite transient and usually get created/destroyed many times during application running time. So...

how can I build my application so I can use one AsyncTask from all activities which will download some data over internet

In my opinion, the most reasonable solution here is Service, obviously service has a more solid life cycle than Activity/AsyncTask. You can implement a service that download data in thread, make it keep running during application running time, and update your current foreground activity's UI stuff from service. check out more details in this SO question.

Hope that help.

Community
  • 1
  • 1
yorkw
  • 40,926
  • 10
  • 117
  • 130