AsyncTask - 从 doInBackground 返回到执行 onPostExecute 之间的时间很长(使用 PagerAdapter)

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

我正在使用 AsyncTask 从 Internet 下载图像。我在 ViewPager 中展示它们。

我从 MyPagerAdapter 中的 instantiateItem 方法调用它使用:

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

我的下载器看起来像:

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);
        }

    }

问题是,从 doInBackground 返回到进入 onPostExecute 大约需要 2000 毫秒。

奇怪的是,当我稍后(在加载第一张图像后)滑动 ViewPager 时,它正常工作并需要大约 0-5 毫秒。

你知道它是由什么引起的吗?

好的,我找到了解决方案。问题出在片段中——当我使用 ViewPager 开始新的 activity 时,保存旧片段的包花费了很多时间(有许多对象的列表作为可序列化的)。我通过查看 DDMS 方法分析注意到了这一点。没有自己的方法,所以我想可能是分片操作的东西。