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 overriding onSaveInstanceState
and checking the parameter of onCreate
.
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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…