简体   繁体   中英

Prevent app from restarting during orientation change

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.

PS 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.

Use this in you AndroidMenifest.xml

<activity
            android:name="MyActivity"
            android:configChanges="orientation|keyboard|keyboardHidden"
            android:screenOrientation="sensor" />

在清单中写下这段代码:

android:configChanges="orientation|screenSize|keyboardHidden"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM