Android - Dealing with a Dialog on Screen Orientation change

后端 未结 3 1594
北恋
北恋 2021-02-20 16:44

I am overriding the onCreateDialog and onPrepareDialog methods or the Dialog class.

I have followed the example from Reto Meier\'s Professional Android Application Devel

相关标签:
3条回答
  • 2021-02-20 16:53

    If you don't want the activity to be recreated when orientation changes. Insert the following line in the AndroidManifest.xml.

    android:configChanges="keyboardHidden|orientation
    

    example:

    <activity android:name=".FABSLogin" android:label="@string/app_name" android:theme="@style/Theme.NoWindowTitle" android:noHistory="true" **android:configChanges="keyboardHidden|orientation**"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

    0 讨论(0)
  • 2021-02-20 16:54

    I don't have Reto's book so I can't check the code, but if there is a mistake in the book you can contact him easily on Twitter to discuss it: http://twitter.com/retomeier

    Remember that if the screen orientation changes, your activity is recreated. That means that any variables you set the last time around, which are not static, will be lost after the activity is recreated under the new orientation.

    My guess is that you hit a NullPointerException here:

    if(setting.getAddForPublicUserNames() == 1){
    

    If so, it is most likely that some user action changes the "setting" variable to something other than null - you need to make sure this is set again when the activity is recreated.

    The most standard method to store/retrieve the activity state between orientations is detailed here: Saving Android Activity state using Save Instance State

    0 讨论(0)
  • 2021-02-20 16:56

    When you change orientation, android restart your activity, you need to save the estate with

    onSaveInstanceState
    

    and recover with

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