Android onCreate() Method called twice when device rotated. (ICS)

后端 未结 3 835
余生分开走
余生分开走 2021-01-14 05:35

I\'m developing on an android tablet application and I have to manage application orientation. I got a problem that every time when I rotate the device onCreate() will be ca

相关标签:
3条回答
  • 2021-01-14 06:32

    If you are targeting an API level > 13 you have to add the screenSize flag as reported in the doc:

      android:configChanges="keyboardHidden|orientation|screenSize"
    
    0 讨论(0)
  • 2021-01-14 06:35

    onCreate() will always be called because when orientation changes the Activity is always recreated. There is nothing wrong with that. You should implement proper behaviour for this or disable the possibility of changing the orientation by adding orientation=horizontal to manifest for instance

    0 讨论(0)
  • 2021-01-14 06:40

    Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must declare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

    Reference: https://developer.android.com/guide/topics/resources/runtime-changes.html

    0 讨论(0)
提交回复
热议问题