Kamil Nękanowicz
Kamil Nękanowicz

Reputation: 6552

Order of Operations in android, why are so going on?

I have a strange problem with the order of execution methods. A progress bar.setVisibility(View.VISIBLE)appears only after the operation geocoder.getFromLocation is end.

summarizing:

my expectations:

Firstly, I want to see progress , later start time-consuming operations

void doSomethingTimeConsuming()
{
   progressWheel.setVisibility(View.VISIBLE);
   for(int i=0;i<100;++i)
   {
       List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
       progressWheel.setProgress(i);
   {
}

program must be running on the main thread without thread

Yes, of course I know that I should not do so in the main thread, I ask because I'm curious about why there was such a strange situation, I would like to understand better to avoid such of situations in the future.

Upvotes: 0

Views: 171

Answers (2)

Nativ
Nativ

Reputation: 3150

Your UI thread is blocked on this line:

geocoder.getFromLocation

You should perform it on a different thread(consider execute it using handler).

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500815

program must be running on the main thread without thread

Well that's the problem. You're performing an expensive operation (getFromLocation) on the UI thread. Don't do that, basically. You're stopping the UI from being updated because the UI thread is busy calling getFromLocation.

You should use another thread for this operation - marshalling back to the UI thread where appropriate to update the UI.

Upvotes: 2

Related Questions