17

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?

james
  • 26,141
  • 19
  • 95
  • 113
  • [here is the answer][1] I think that is the solution you are looking for. [1]: http://stackoverflow.com/a/3821998/1665247 – dvrm Jun 27 '13 at 09:50

6 Answers6

15

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

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
  • this did it! silly of me, i should have known this was the fix. – james Sep 20 '10 at 17:53
  • 8
    I had to add "screenSize" to this for targeting API level 13 and above. Otherwise it doesn't work for those environments. android:configChanges="orientation|screenSize" – notmystyle Apr 08 '13 at 18:49
  • Thanks for the screenSize hint! I had "orientation|keyboard" and suddenly it didn't work anymore. This works perfectly, thanks! – TSGames Sep 11 '13 at 09:25
6

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
  • 7,059
  • 6
  • 31
  • 58
  • 2
    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 Sep 13 '10 at 14:19
  • What if my AsyncTask is not in my activity... Its in another class file common to many activity class. Then how can I get setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); – Debopam Mitra Aug 14 '12 at 12:42
  • Great but to lock the orientation ActivityInfo.SCREEN_ORIENTATION_LOCKED works better for me, especially in landscape. – NiPfi May 12 '14 at 16:26
4

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.

Community
  • 1
  • 1
Josh
  • 10,618
  • 2
  • 32
  • 36
2

Add this in activity tag in application manifest.xml

<activity android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize" android:name=".your.package"/>

Deepak
  • 61
  • 1
  • 4
1

In my case I have used

android:configChanges="orientation" 

but it did not work for me

Following is working fine

<activity android:name=".MyActivity" 
          android:configChanges="orientation|screenSize|screenLayout">
</activity>
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
-1

You can use the following code in Your Manifest

<activity android:label="@string/app_name" 
        android:configChanges="orientation|keyboardHidden" 
        android:name=".your.package">
Datta Kunde
  • 467
  • 6
  • 14