Search code examples
androidperformanceandroid-asynctaskandroid-viewpagerandroid-pageradapter

AsyncTask - long time between returning from doInBackground and execution of onPostExecute (with PagerAdapter)


I'm using AsyncTask to download images from the Internet. I'm showing them in ViewPager.

I call it from instantiateItem method in MyPagerAdapter using:

downloader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,imageView, url);

My downloader looks like:

protected Bitmap doInBackground(Object... params) {

        //(...)
        //downloading image using httpConnection and decoding it to bitmap using BitmapFactory
        //(...)

        Log.d("myApp", "returning");
        start = System.currentTimeMillis();
        return bitmap;

    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        long stop = System.currentTimeMillis();
        long period = stop-start;
        Log.d("myApp-trace","it took: "+period);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }

    }

The problem is, it takes about ~2000 miliseconds between returing from doInBackground and entering onPostExecute.

What is strange, when I swipe the ViewPager later (after the first images are loaded) it works normally and takes ~0-5 miliseconds.

Do you have any ideas what can it be caused by?


Solution

  • Ok, I've found a solution. The problem was in Fragments - when I was starting new activity with ViewPager, it took a lot of time to save old fragments' bundles (there was a list of many objects as Serializable). I noticed that by looking into DDMS method profiling. There was no my own methods, so I thought it may be something with fragments operations.