I have the following requirement:
Need to fetch data from server.
Re-size multiple images which is fetched from server & render on screen.
My Approach
I am using the following code
private void fetchResult() {
mProgressDialogue = ProgressDialog.show(this, "",
"Fetching results ... Please wait");
new Thread() {
public void run() {
Message msg = Message.obtain();
try {
mResponseVec = new Vector();
Object getItemsResponseObj = loadCateg开发者_开发问答oryResponse();
mResponseVec = Utilities.getInstance()
.parseGetItemsResponse(getItemsResponseObj);
} catch (Exception e) {
}
messageHandler.sendMessage(msg);
}
}.start();
}
protected Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
showProduct(mProductName, mResponseVec, mProductId, 0, 0);
mProgressDialogue.dismiss();
}
};
Within the showProduct()
method, I am resizing around 6 images after calling the images imageUrl
. This resizing of the images is taking some time. The problem I am facing with the above code is that, after fetching the data from the server, during the time for resizing, the progressdialog is visible but it becomes static (stop rotating) & stays on that non rotating position for some time & then the showProduct
screen is displayed.
My requirement is I want the progressDialog to remain & rotate till the showScreen
is displayed.
Will be great if anyone can provide their inputs.
It seems you create the messageHandler from your UI thread (assuming fetchResult runs in the UI thread). That binds it to the UI thread and its message queue. So, when your new thread sends a message to the messageHandler, the messageHandler is executed in the UI thread, blocking your progress dialog.
Try splitting up showProduct into one part that does the fetching and resizing, and another part that displays the results. Then move the fetching and resizing part into your thread and send the message to your UI thread only after the resizing has finished. The messageHandler then only has to do the second part, i.e. displaying the images.
Cheers tadzio
if you wanted the progress dialog to go away before showProduct(), why did you put the dismiss after it in your code?
精彩评论