When Android Lollipop was released, I was excited. Who wouldn’t? Every new version always comes packed with new features and enhancements.

Sad to say when I upgraded to Lollipop it gave my app problems instead. Two things my app used, an XML drawable that displays a radial gradient effect and an animated inside an AsyncTask class that uses Thread.sleep in between.

So first, the radial drawable.

I had a custom color and named it theme_green. It worked okay in previous versions but in Lollipop 5.0, the radial effect does not work anymore, and so does the color. It seems like only the startColor is used hence the different color result.

The other issue I had was the Thread.sleep inside the doInBackground() where I had a for loop that shows an animated and used the Thread.sleep to simulate a pause in between animation.

The result was that the animated got slow. I had to switch to using a TimerTask instead to preserve the smoothness of the animated that was working prior to Lollipop 5.0.

Oh, and even SystemClock.sleep() inside doInBackground() did not work either.

So there. Just to give you a heads up that you might think it is the Lollipop 5.0 operating system that is the issue. They definitely changed some things that affected how the Thread.sleep() works inside the doInBackground() method of an AsyncTask class.

Well, here’s the thing. The execute() method of AsyncTask were first introduced to execute serially on a single background thread.

Then it was changed to a pool of threads allowing multiple tasks to operate in parallel beginning with Android Donut OS.

With HoneyComb, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

So if you want to run 2 or more tasks asynchronously, you use the method executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

If your app supports older versions prior to HoneyComb, you can do this kind of check:

Honestly, I am glad I got this to work the first time. Otherwise, I totally would have been stuck for who knows how long.

It probably had been more than 2 years since I used ListView and modified its contents like adding, removing and updating. When I made another go recently, I was confused why the ListView does not refresh its content.

I thought calling the notifyDataSetChanged() of ArrayAdapter after add() or remove() will do the trick in the onPostExecute() of the AsyncTask class.

The solution is not to use the add() or remove() method of the ArrayAdapter class. Instead, you place a global variable in your class for the ArrayList object that you will pass to the ArrayAdapter.

I instantiate an ArrayAdapter like this:

Make sure the myArrayList variable is global. So that within your AsyncTask class, you can call add() or remove() method of the ArrayAdapter class within doInBackground() of AsyncTask and call adapter.notifyDataSetChanged() onPostExecute().

That should refresh your ListView contents.

See sample below:

Related Posts Plugin for WordPress, Blogger...