proper design when need to complete long task before going to next in Android

81 Views Asked by At

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.

4

There are 4 best solutions below

1
On BEST ANSWER

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 in doInBackground. Then update Activity B with your Venus info and hide your ProgressBar in onPostExecute.

0
On

You can look into ASYNC TASK. Go through examples of how to load images from url in a listview. Such examples get url in json format and it is parsed into array list and stored in listview

0
On

You should use android AsyncTask to download all the data from the server. And you can show the user a progress bar while he is waiting.

You can find examples on the links that I shared.

0
On

You should show progress of your web data retrieval somehow. This actually depends on your UI design. You can show ProgressDialog, or some spinning drawable on your list item. This also depends on what you allow to do during this time, if choosing other item is allowed then this adds additional complexity - because you would have to cancel previous download, and start new one.

It might happen also that server is inaccessible, and connection might take even 1 minute, and then fail. Also it might happen that connection is very slow. In such cases user might want to cancel operation, by pressing some button on you GUI or by pressing back button - this last makes sense if you are using ProgressDialog.

Other solution is to show new activity, where details should appear, and instead show large spinning wheel (intdeterminate progress drawable), pressing back will cancel download and go back to previous activity/fragment.