1

Possible Duplicate:
How to handle an AsyncTask during Screen Rotation?

What is the proper way to handle AsyncTask operations during a screen orientation change?

Currently I am overriding onRetainNonConfigurationInstance() and storing stuff inside of a hashmap and checking in onCreate() if getNonConfigurationInstance() != null... I read up on dev.android and may have missed something..

My AsyncTask is handling Internet tasks..fetching images etc... Below is my AsyncTask, as for my onRetainNonConfigurationInstance I am grabbing storing getText() results from TextViews as well as storing a String [] and ImageView inside a HashMap

I am starting the AsyncTask new DownloadImage().execute(imgarr);

private class DownloadImage extends AsyncTask{

    @Override
    protected Drawable[] doInBackground(String[]... arg0) {
        // TODO Auto-generated method stub
        InputStream is = null;
        Drawable [] drawarr = new Drawable[arg0[0].length];
        int size = arg0[0].length;
        if(size > 20){
            size = 20;
        }
        try {
            for(int i = 0; i < size; ++i){
            is = (InputStream) new URL(arg0[0][i]).getContent();
            drawarr[i] = Drawable.createFromStream(is, "pic");
            }
        } catch (MalformedURLException e) {Log.v("AdImageDownload","DownloadImage AsyncTask Malformed URL",e.getCause());} 
        catch (IOException e) {Log.v("AdImageDownload","DownloadImage IOException",e.getCause());}
        catch(NullPointerException e){Log.v("AdImageDownload","DownloadImage IOException",e.getCause());}
        return drawarr;
    }

    @Override
    protected void onPostExecute(Drawable[] result) {
        // TODO Auto-generated method stub
        //img1.setImageDrawable(result);
        for(int i = 0; i < result.length; ++i){
            img1 = (ImageView)findViewById(imgIds[i]);
            img1.setImageDrawable(result[i]);
        }
        super.onPostExecute(result);
    }

}
Community
  • 1
  • 1
Andrew
  • 136
  • 2
  • 11
  • Did you add in your manifest `android:configChange="something"`? – Nikola Despotoski Aug 29 '11 at 22:02
  • @Nikola Despotoski I did not.. But, then I would override onConfigurationChanged() instead of onRetain? – Andrew Aug 29 '11 at 22:06
  • Correct. AsyncTask is crashing because the activity is restarted thus the threads are killed, and from the threading rules that says that AsyncTask cannot be resume, any attempt to resume will result crash. :) And word for `onCC()` if you override it, the activity will not restart. Test yourself, though – Nikola Despotoski Aug 29 '11 at 22:10
  • k...lemme mess around with it. – Andrew Aug 29 '11 at 22:14
  • 1
    I think officially it is not recommended to use configChanges unless you really have to. It is perfectly fine to use onRetainNonConfigurationInstance() with an AsyncTask (or with a retained fragment in Android >=3.0) to handle the orientation change. If you post some code maybe we can figure out why it is crashing? http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange http://stackoverflow.com/questions/2620917/how-to-handle-an-asynctask-during-screen-rotation/2624569#2624569 – antonyt Aug 29 '11 at 22:22
  • @antonyt, thats simplest way to prevent the activity restarting itself. As I said before AsyncTask is killed then as well. – Nikola Despotoski Aug 29 '11 at 22:33
  • The AsyncTask is not killed if you use onRetainNonConfigurationInstance() / retained Fragment. You can let the activity restart itself as normal but preserve the stuff you want (in this case, the AsyncTask). That's the point of that method. – antonyt Aug 29 '11 at 22:44
  • @antonyt I edited my original post. After reading your last comment about onRetainNonConfirguationInstance(). I am not storing anything related to AsyncTask. If I am starting AsyncTask with just a new taskname().execute(var); should I be storing inside an AsyncTask var and then placing inside my HashMap? – Andrew Aug 29 '11 at 22:47
  • I think I see inside the page you referenced - A scenario in which this can be valuable is if your application loads a lot of data from the web. If the user changes the orientation of the device and the activity restarts, your application must re-fetch the data, which could be slow. What you can do instead is implement onRetainNonConfigurationInstance() to return an object carrying your data and then retrieve the data when your activity starts again with getLastNonConfigurationInstance().. Will play around – Andrew Aug 29 '11 at 22:59
  • It looks like your AsyncTask is probably an inner class of your Activity? This means it's going to have a reference to an instance the Activity and this will definitely result in a leak if you try to use OnRetainNonConfigurationInstance() (a new Activity is created, but the old one is being kept alive because of the AsyncTask). You shouldn't persist anything that has a reference to the Activity - this includes Views of all kinds. I think you will have to do a bit of re-designing to get this to work properly - this is why a lot of people just take the lazy route and use configChanges! – antonyt Aug 29 '11 at 23:05
  • Can I ask you about this statement: new Drawable[arg0[0].length]; arg0 is a String array, so arg0[0] is the first element of that array. Strings don't have a length member variable (only the method length()) so how does this code even compile? And do you not really want to just do arg0.length instead? – antonyt Aug 29 '11 at 23:05
  • @anthonyt I can resdesign and stick the AsynTask's outside if thats what needs to be done, that is not a big deal. arg0[0] is a String array arg0 is the arg that android passed in the first element of that array is a string array, I think you are thinking.. arg0[0][0] would be first element of the string array...:/ – Andrew Aug 30 '11 at 01:21
  • @anthonyt I was able to get all that going, any idea why progress dialog doesn't show anymore? Passing a ProgressDialog to each individual class extending asynctask... – Andrew Aug 30 '11 at 03:20
  • @anthonyt I can not get the actual Dialog to spin anymore it has disappeared and is not showing.. – Andrew Aug 30 '11 at 04:02

0 Answers0