views:

57

answers:

3

I am showing a ProgressDialog in the onPreExecute method of an AsyncTask object and canceling the ProgressDialog in the onPostExecute method. In the doInBackground method I am making an HTTP request for user registration. I wish to allow screen orientation changes. When I change the orientation while the doInBackground method is still running, i get all sorts of fun errors like 'IllegalArgumentException: View not attached to window manager' and 'RegisterScreen has leaked window...'

How can I properly continue to show the ProgressDialog after an orientation change? Or maybe, how can I disable orientation change after the user requests to submit their registration?

+1  A: 

You could try disabling orientation changes during the time you show the ProgressDialog.

at the beginning do:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and enable back after completion:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

Hope this helps.

(If anyone has a proper solution, I would also be interested :-)

Sec
The problem isn't orientation changing. You're seeing these issues because of the activity lifecycle, which destroys and recreates your activity. Preventing orientation changes will prevent this particular instance of the lifecycle, but the same issue will crop up when the user presses 'Home' during the ProgressDialog and returns. Your activity should be designed to close down and restart at any time.
Josh
+1  A: 

You want to properly handle the activity lifecycle, which means saving and restoring the state of your activity, not attempting to prevent lifecycle changes. Do some reading on AsyncTask vs. the activity lifecycle.

For example: pause-and-resume-asynctasks-android and what-to-do-with-asynctask-in-onpause.

Josh
+1  A: 

Try adding this attribute android:configChanges="orientation" to your Activity element in the Android.manifest file.

Ragunath Jawahar
this did it! silly of me, i should have known this was the fix.
binnyb