문제

For the down voters,it would be better if you could provide a working solution,not every question need to have a code attached with it,if one is not clear with the concepts how can u expect him to provide you with the code he played with??

This is basically a conceptual question,i tried reading docs but still i couldn't get a better understanding of the topic. i am not sure how i should use async task.... i have used the async task before for displaying an image from internet, but i am still confused how it works.I know 3 of its functions that are used commonly. i.e

1.onPreExecute () 2.doinBackground() 3.onPostExecute()

now i am confused that if i have to populate a list how should it be done?? I know The populating part should be done in the doinbackground(),but after that should i return the result (from the background),after the whole list has been populated, to onPostExecute() and expect that the list will be loaded on the listview asynchronously
or should i return the result in parts(say a new item has been added to the list,send it to the onpostexecute immediately without waiting for the whole list to be generated, to be displayed and repeat the iteration ) to the onpostExecute()??and manage the lazy load ourselves by doing so?

도움이 되었습니까?

해결책

well.. why don't you see this below android API information..

The 4 steps When an asynchronous task is executed, the task goes through 4 steps:

  1. onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

  2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

onPreExecute() method is used when starting asynktask function.. typically in this method, someone use progress dialog..

and doInBackground() methos is used when progress dialog is running. in this method you can implement your job(function) that you want. I think this part is the most important point among methods of this asynktask class

and onPostExecute() method is typically used when finished background job.. in order to deliver some kind of data or result.. to View

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top