Android - GridView's onResume
<p>I am developing Android apps, </p>

<p>once part of this apps is creating a GridView that contain some image (the image is loaded from URL),</p>

<p>then another activity that show the selected image in full screen will be appeared when one of image in GridView is onClick.</p>

<p>Problem:</p>

<p>When I enter the GridView activity, it takes some second to load all image in gridview normally. </p>

<p>Then I click one of image to enter the full screen activity and click back button to go back to GridView,</p>

<p>but, it takes some second to loading when go back to gridview, just like loading all image again.</p>

<p>I wonder why the gridview activity will loading for a few second when onResume?</p>

<p>For example, in Google Play, the full screen view of sample image in any apps can be back to previous view immediately.</p>

<p>Enclosed code:</p>

<p>GridView:</p>

<pre><code>public class ManagePhoto extends Activity {

ImageAdapter ia;
GridView gridview;
InputStream inputStream;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
TextView tvRunningMark;
boolean bRunning;
String[] purl;
Bitmap[] bm;
String the_string_response;
TouchImageView touch;
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.photo_manage);

    //gv is Gobal Value
    final Gvalue gv = ((Gvalue) getApplicationContext());

    gridview = (GridView) findViewById(R.id.gv_photo);
    gridview.setOnItemClickListener(new GridView.OnItemClickListener() {
        public void onItemClick(AdapterView adapterView, View view,int position, long id) {
            gv.setbm(bm[position]);
            Intent myIntent = new Intent(adapterView.getContext(), FullScreenImage.class);
            startActivityForResult(myIntent, 0);
        }
    });
    new GridTask().execute();
}

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return purl.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(200,200));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        try {
            bm[position] = loadBitmap(purl[position]);
            imageView.setImageBitmap(bm[position]);

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return imageView;
    }
}

class GridTask extends AsyncTask&lt;Void, String, Void&gt; {

    @Override
    protected void onPostExecute(Void result) {
        gridview.setAdapter(ia);
        final LinearLayout llo_probar = (LinearLayout)findViewById(R.id.llo_probar);
        llo_probar.setVisibility(LinearLayout.GONE);
        gridview.setVisibility(GridView.VISIBLE);
    }

    @Override
    protected void onPreExecute() {         
    }

    @Override
    protected void onProgressUpdate(String... values) {
    }

    @Override
    protected Void doInBackground(Void... params) {         
        getphoto();
        bm = new Bitmap[purl.length];
        ia = new ImageAdapter(ManagePhoto.this);
        return null;
    }

}

private Bitmap loadBitmap(String url) throws MalformedURLException,IOException {
    return BitmapFactory.decodeStream(new FlushedInputStream(
            (InputStream) new URL(url).getContent()));
}

class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(final InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(final long n) throws IOException {
        long totalBytesSkipped = 0L;

        while (totalBytesSkipped &lt; n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);

            if (bytesSkipped == 0L) {
                int bytesRead = read();

                if (bytesRead &lt; 0) { // we reached EOF
                    break;
                }

                bytesSkipped = 1;
            }

            totalBytesSkipped += bytesSkipped;
        }

        return totalBytesSkipped;
    }
}

public void getphoto(){
    final Gvalue gv = ((Gvalue) getApplicationContext());
    final TextView tv_fn = (TextView) findViewById(R.id.tv_fn);
    String result = "";
    ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
    nameValuePairs.add(new BasicNameValuePair("sql",
            ("select * from personal_photo where member_id = " + gv.getuid())));

    InputStream is = null;
    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://" + gv.getserverIP()
                + "/android_getdata.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();

        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    // parse json data
    try {
        List url = new ArrayList();
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i &lt; jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            url.add(json_data.getString("save_location"));
        }
        int size = url.size();
        purl = new String[size];
        for (int j = 0; j &lt; size; j++) {
            purl[j] = (String) url.get(j);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
}

public void toast(String text) {
    Toast.makeText(ManagePhoto.this, text, 5).show();
}
}
</code></pre>

<p>Full screen:</p>

<pre><code>public class FullScreenImage extends Activity {
TouchImageView touch;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    new ShowPhotoTask().execute();
}

class ShowPhotoTask extends AsyncTask&lt;Void, String, Void&gt; {


    @Override
    protected void onPostExecute(Void result) {
        setContentView(touch);
    }

    @Override
    protected void onPreExecute() {
        setContentView(R.layout.full_image);
    }

    @Override
    protected void onProgressUpdate(String... values) {
    }

    @Override
    protected Void doInBackground(Void... params) {
        final Gvalue gv = ((Gvalue) getApplicationContext());
        touch = new TouchImageView(FullScreenImage.this);
        touch.setMaxZoom(4f); // change the max level of zoom, default is 3f
        touch.setImageBitmap(gv.getbm());
        return null;
    }

}
}
</code></pre>
