Don't reload application when orientation changes
Solution 1:
There are generally three ways to do this:
As some of the answers suggested, you could distinguish the cases of your activity being created for the first time and being restored from
savedInstanceState
. This is done by overridingonSaveInstanceState
and checking the parameter ofonCreate
.You could lock the activity in one orientation by adding
android:screenOrientation="portrait"
(or"landscape"
) to<activity>
in your manifest.You could tell the system that you meant to handle screen changes for yourself by specifying
android:configChanges="orientation|screenSize"
in the<activity>
tag. This way the activity will not be recreated, but will receive a callback instead (which you can ignore as it's not useful for you).
Personally I'd go with (3). Of course if locking the app to one of the orientations is fine with you, you can also go with (2).
Solution 2:
Xion's answer was close, but #3 (android:configChanes="orientation"
) won't work unless the application has an API level of 12 or lower.
In API level 13 or above, the screen size changes when the orientation changes, so this still causes the activity to be destroyed and started when orientation changes.
Simply add the "screenSize" attribute like I did below:
<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize">
</activity>
Now, when you change orientation (and screen size changes), the activity keeps its state and onConfigurationChanged()
is called. This will keep whatever is on the screen (ie: webpage in a Webview) when the orientation changes.
Learned this from this site: http://developer.android.com/guide/topics/manifest/activity-element.html
Also, this is apparently a bad practice so read the link below about Handling Runtime Changes:
http://developer.android.com/guide/topics/resources/runtime-changes.html