3

I have a canvas, and it is redrawn when orientation is changed. It's a custom canvas not android provided.

When screen layout is changed, application state and (all of it's view's states) gets reset. I changed screen orientation to portrait only; it keeps screen layout unchanged but application is again resetted.

I checked documentation and found that activity is destroyed and restarted again when orientation change occurs. Savestate() cannot save and load bitmap data or any large data which are required by my custom canvas.

I again checked documentation, and found Handling run time changes topic, which mentioned onConfigurationChanged() which gets called when specific configuration change is occurred, which in my case is 'orientation'. This method prevents restart and leaves to developer how that configuration change should be implemented. It even mentioned in last paragraph that if I will not implement that method then this will just cause the activity to skip onRestart() and will do nothing. I am setting manifest file as

android:screenOrientation="portrait"
android:configChanges="orientation"

And I am not implementing onConfigurationChanged(). But this doesn't help either. I don't know why. It seemed so helpful to me.

Please post solution if you have any. Also, app takes some resonable time, and I would like app do not restart when orientation is changed. Actually I don't want to do anything when this happens. I am using emulator too, so please clarify if it is emulator only problem.

P.S. My internet connection is down and I am using my stupid mobile client. I have checked the offline documentation. And please bear with me for spellings. I am trying to find solution, but currently I am stumpped.

Master Chief
  • 2,380
  • 15
  • 27

3 Answers3

7

Use this in you AndroidMenifest.xml

<activity
            android:name="MyActivity"
            android:configChanges="orientation|keyboard|keyboardHidden"
            android:screenOrientation="sensor" />
Arslan Anwar
  • 18,486
  • 18
  • 74
  • 104
  • That solved, actually I think problem was emulator was showing keyboard which resulted in app restart. I should have put the values for keyboard too. Stupid emulator. – Master Chief May 08 '12 at 14:02
  • I am not able to mark it as the answer from my mobile client, will mark it as soon as I can. – Master Chief May 08 '12 at 14:09
1

write this code in manifest:

android:configChanges="orientation|screenSize|keyboardHidden"
mojtaba
  • 86
  • 2
0

For each activity in the AndroidManifest, the screenOrientation can be specified. For example, to specify that the activity always stays in portrait mode, the following can be added to the activity element:

android:screenOrientation="portrait"

Similarly, landscape mode can be specified using the following:

android:screenOrientation="landscape"

However, the previous still causes the activity to be destroyed and restarted when a hard keyboard is slid out.Therefore, a third method is possible:Tell the Android system that the application should handle orientation and keyboard slide-out events.This is done by adding the following attribute to the activity element: android:configChanges="orientation|keyboardHidden"

Daniel Conde Marin
  • 7,055
  • 4
  • 30
  • 40