I new to android programming and I have a question regarding this need to perform long tasks in the background. I have this application that displays a list of say forum topics in listview in Activity A and when a user click an item on this list I need to display information about this item in Activity B, which includes some text and some images.
Now, this information is received from the web. Since this is communication task I should put it in a service of some sort or I might encounter an ANR.
My question is - what do I do until I receive the information. Since I can not display Activity B before getting all the information I must "hang" in Activity A until I get the information from the Web.
Can anyone instruct me about the proper design pattern for such a common flow? Some code would be very helpful too, of course.
Thanks in advance.
When the user clicks on an item in Activity A, launch Activity B. Pass a parameter into the intent of Activity B to be the subject of the detailed view. For example, Activity A lists planets. User clicks on "Venus" and launches an intent to start Activity B with the parameter "Venus".
Activity B will then launch an AsyncTask which will query a server for all the information you need to display about Venus in Activity B. During the query, you can display a progress bar. After, you finish parsing the JSON response from the server, update your Activity B with your Venus info.
Here is a link to an example of how to use AsyncTask. Following the example I've laid out, you will show a ProgressBar in
onPreExecute
. Then, query the server indoInBackground
. Then update Activity B with your Venus info and hide your ProgressBar inonPostExecute
.