java - Where can I write my AsyncTasks -


i'm creating application makes extensive use of asynctask retrieve data database. problem i've got 12 different asynctasks , it's making code dirtier , dirtier, plus fact tasks shared among different activities.

some of asynctask i've written have own java class file, it's not ideal since requests need access application's resources (making use of resources.getsystem().getstring(stringid) impossible, or requiring pass instances need modify parameters reference)

is there recommended way write asynctask classes ?

its hard without knowing exact circumstances if have similar tasks can create them in own file , create constructors take parameters need.

you can set them accept context, activity object, etc...then make them reusable. if have necessary activity make inner class if used multiple activities make separate class , use constructor(s) take needed parameters.

i have many asynctasks in applications , depends on how need them. said, can pass context constructor when need objects require context. pass like

mytask task = new mytask(this);  // passing context or other params constructor takes task.execute(); 

and in task

public class mytask extends asynctask<...>  // add asynctask params {      context context;      public mytask(context c)      {          context = c;      }      // methods } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -